Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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
Reactjs 在react js中使用ProtectedRoutes时如何停止窗体值重置_Reactjs_React Router - Fatal编程技术网

Reactjs 在react js中使用ProtectedRoutes时如何停止窗体值重置

Reactjs 在react js中使用ProtectedRoutes时如何停止窗体值重置,reactjs,react-router,Reactjs,React Router,我的项目中有一个antd表单组件。该组件由我创建的ProtectedRoute保护 //ProtectedRoute const ProtectedRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={({ location }) => auth.isAuthenticated ? ( <C

我的项目中有一个antd
表单
组件。该组件由我创建的
ProtectedRoute
保护

//ProtectedRoute 
const ProtectedRoute = ({ component: Component, ...rest }) => (
      <Route
        {...rest}
        render={({ location }) =>
          auth.isAuthenticated ? (
            <Component {...rest} />
          ) : (
            <Redirect
              to={{
                pathname: "/account/login",
                state: { from: location },
              }}
            />
          )
        }
      />
    );


//Route
return (
   <Router>
      <Switch>
          <ProtectedRoute exact path="/" component={CaseSearchPage} />
           //I have another set of routes here
      </Switch>
   </Router>
);
//受保护的路由
const ProtectedRoute=({component:component,…rest})=>(
验证了吗(
) : (
)
}
/>
);
//路线
返回(
//我这里还有一套路线
);
但是在
CaseSearchPage
组件中,当我切换语言时,表单数据被重置。当我使用
Route
而不是
ProtectedRoute
时,它会按预期工作。有办法解决这个问题吗?
提前谢谢

好的,我想好了

因为我使用的是受保护的路由,所以它会重新渲染整个组件。因此,即使是状态值也会被清除。为了解决这个问题,我稍微修改了上面的代码

//New protected route
const ProtectedRoute = ({ component: Component, ...rest }) => (
   <Route
      render={({ location }) => (
        <Redirect
           to={{
              pathname: "/account/login",
              state: { from: location },
           }}
         />
      )}
   />
);


//new Route
return (
   <Router>
      <Switch>
         <Route
            exact
            path="/"
            component={auth.isAuthenticated ? CaseSearchPage : ProtectedRoute}
         />         
      </Switch>
   </Router>
);
//新的受保护路由
const ProtectedRoute=({component:component,…rest})=>(
(
)}
/>
);
//新路线
返回(
);
我现在在
Route
中检查它,而不是在
ProtectedRoute
中检查
auth.isAuthenticated
ProtectedRoute
中唯一的内容是
重定向
路由