如何重新渲染不同的 React 功能组件?

问题描述

我正在尝试解决如何重新渲染不同的功能组件。

onComplete: function (data) {
        console.log("everything is complete");
        console.log("this is the applicant id",id);               
        let obj;
        axios
          .post("http://localhost:5000/post_id",{
            applicant_id: id,})
          .then((response) => {
            obj = response.data.data.data.json_data.result;
          });
        setTimeout(() => {
          if (obj === "clear") {
            onfidoOut.tearDown(); // <- the Onfido view did not unmount
            renderResult();  // <- BAD APPROACH? `renderResult` renders a HTML node instead of triggers a view rerender.
          }
        },3000);
      },

我开始采用一种渲染 HTML 节点而不是触发视图重新渲染的方法,因为我从未在基于类的组件中的身份验证流程之外实现对不同组件的视图重新渲染。这也是一个微前端应用程序。这是获取上述代码段的整个文件

const uSEOnfidoFetch = (URL) => {
  const [token,setToken] = useState();
  const [id,setId] = useState();

  useEffect(() => {
    axios
      .get("http://localhost:5000/post_stuff")
      .then((response) => response.data.data.data.json_data)
      .then((json_data) => {
        console.log("this is the json data",json_data);
        const id = json_data.applicant_id;
        const token = json_data.onfido_sdk_token;
        setId(id);
        setToken(token);
      });
  },[URL]);

  useEffect(() => {
    if (!token) return;
    console.log("this is working!");
    OnfidoSDK.init({
      token,containerId: "root",steps: [
        {
          type: "welcome",options: {
            title: "Open your new bank account",},"document",],onComplete: function (data) {
        console.log("everything is complete");
        console.log("this is the applicant id",id);
        let obj;
        axios
          .post("http://localhost:5000/post_id",})
          .then((response) => {
            obj = response.data.data.data.json_data.result;
          });
        setTimeout(() => {
          if (obj === "clear") {
            onfidoOut.tearDown();
            renderResult();
          }
        },});
  },[id,token]);

  function renderResult() {
    return <Redirect to="/result" />;
  }
};

export default function() {
  const URL = `${transmitAPI}/anonymous_invoke?aid=onfido_webapp`;
  const result = uSEOnfidoFetch(URL,{});

     return (
        <div id={onfidoContainerId} />
     )
}

这是 bootstrap.js 文件

import React from 'react';
import ReactDOM from 'react-dom';
import {createMemoryHistory,createbrowserHistory} from 'history';
import App from './App';

// Mount function to start up the app
const mount = (el,{ onNavigate,defaultHistory,initialPath}) => {
  const history = defaultHistory || createMemoryHistory({
     initialEntries: [initialPath],});

  if (onNavigate) {
    history.listen(onNavigate);
  }

  ReactDOM.render(<App history={history} />,el);

  return {
    onParentNavigate({ pathname: nextPathname }) {
      const {pathname} = history.location;

      if (pathname !== nextPathname) {
        history.push(nextPathname);
      }
    },};
};

// If I am in development and isolation
// call mount immediately
if (process.env.NODE_ENV === 'development') {
  const devRoot = document.querySelector('#_marketing-dev-root');

  if (devRoot) {
    mount(devRoot,{defaultHistory: createbrowserHIstory()});
  }
}

// Assuming we are running through container
// and we should export the mount function
export {mount};

解决方法

似乎obj(来自response)可用于管理状态,例如:

const [receivedResults,setReceivedResults] = useState(false);

然后当收到响应时:

setReceivedResults(obj === "clear");

并使用标志来允许组件呈现自身(不确定确切的语法):

if (receivedResults) {
   return <Redirect to="/result" />;
} else {
   return null;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...