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