如何通过AppRouter传递props
我需要通过 AppRouter 获取 userRole
:
App.js
<BrowserRouter>
<AppRouter
userRole = {userRole}
/>
</BrowserRouter>
AppRouter.js
const AppRouter = observer((userRole) => {
const {user} = useContext(Context)
<Routes>
{user.isAuth && authRoutes.map(({path, Component}) =>
<Route
key={path}
path={path}
element={
<Component
userRole={userRole} //here i pass
/>
}
exact
/>
)}
<Route path="*" element={<Navigate to={JADE_ROUTE} />}/>
</Routes>
这是我的 Component
但 userRole
已经未定义
const CarPage = observer(({userRole}) => {
console.log(userRole)
return <h1>{userRole}<h1/>
}
I need to get userRole
via AppRouter:
App.js
<BrowserRouter>
<AppRouter
userRole = {userRole}
/>
</BrowserRouter>
AppRouter.js
const AppRouter = observer((userRole) => {
const {user} = useContext(Context)
<Routes>
{user.isAuth && authRoutes.map(({path, Component}) =>
<Route
key={path}
path={path}
element={
<Component
userRole={userRole} //here i pass
/>
}
exact
/>
)}
<Route path="*" element={<Navigate to={JADE_ROUTE} />}/>
</Routes>
Here my Component
but userRole
is already undefined
const CarPage = observer(({userRole}) => {
console.log(userRole)
return <h1>{userRole}<h1/>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 AppRouter 组件中,您似乎没有从 props 中提取 userRole 。会是这样吗?
应该是
In your
AppRouter
component it seems you're not extracting theuserRole
fromprops
. Could that be it?should be