Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React应用程序中使用useState更新路由器值时,重新呈现的错误太多_Javascript_Reactjs - Fatal编程技术网

Javascript 在React应用程序中使用useState更新路由器值时,重新呈现的错误太多

Javascript 在React应用程序中使用useState更新路由器值时,重新呈现的错误太多,javascript,reactjs,Javascript,Reactjs,在React应用程序的Controller.jsx文件中,我想更改Route并显示相关组件。像这样: function Controller(props) { const [path, setPath] = useState("/home/"); let componentToShow; function setPathAndComponent(newPath, newComponent) { setPath(newPath); comp

React
应用程序的
Controller.jsx
文件中,我想更改
Route
并显示相关组件。像这样:

function Controller(props) {
  const [path, setPath] = useState("/home/");
  let componentToShow;

    function setPathAndComponent(newPath, newComponent) {
     setPath(newPath);
     componentToShow = newComponent;
    }

  switch (props.args.navigationValue) {
    case 0:
      setPathAndComponent("/home/", <Text />);
      break;

    case 1:
      args = { onClick: showLoginComponent };
      setPathAndComponent("/dashboard/", <Dashboard args={args} />);
      break;

    case 2:
      setPathAndComponent("/admin/");
      break;

    case 3:
      setPathAndComponent("/settings/");
      break;

    default:
      setPathAndComponent("/home/");
      break;
  }

  return (
    <Router>
      <Redirect to={path} />
       <Switch>
        <Route path="/home/">{componentToShow}</Route>
        <Route path="/home/main/">{componentToShow}</Route>
        <Route path="/dashboard/">{componentToShow}</Route>
        <Route path="/login/">{componentToShow}</Route>

      </Switch>
    </Router>
  );
}
export default Controller;

我不知道如何预防这种情况。我需要更改整个项目的根路径值,当新的url更改到达时,它应该触发新的更改。

重定向到路径?重定向到路径?
react-dom.development.js:14815 Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.