【发布时间】:2020-07-23 21:00:43
【问题描述】:
我有一个简单的 React+Hooks webapp,我有一个渲染 componentA 的 route1。 在 ComponentA 上,如果满足条件,我可以为该组件呈现正确的 jsx 或重定向到 Route2。 在 route2 上,我需要从 route1 渲染的 componentA 中获取一些状态。
组件A.jsx
return (
<Fragment>
{!call ?
(
<div className="container">
//some jsx
</div>
)
:
(
<Redirect to={{pathname: '/route2', state: { meeting, name }}}/>
)}
</Fragment>
);
App.js
const App = () => {
return(
<AuthProvider>
<Router history={history}>
<Switch>
<Route exact path="/route1" component={ComponentA} />
<Route exact path="/route2" component={ComponentB} />
</Switch>
</Router>
</AuthProvider>
);
};
我试图在 ComponentB.jsx 上获取状态 props.location.state,但它始终未定义。有没有办法将这些参数传递给路由,还是我需要使用 Context 来解决这个问题?
【问题讨论】:
-
您的会议和姓名可能有误。你放一个 , 而不是 ;
标签: reactjs react-router