reactjs - 使用 HOC 反应路由器渲染方法
问题描述
我试图在 react-router-dom (5.0.1)中实现这个逻辑。这是我的路线:
<Route
path="/available-days"
render={props => isAdmin ? Auth(UserProfileView) : <Redirect to='/dashboard'/>}
/>
什么时候idAdmin
会true
导致错误:Objects are not valid as a React child
. 我也试过这个:
<Route
path="/available-days"
render={props => isAdmin ? Auth(UserProfileView)({ ...props }) : <Redirect to='/dashboard'/>}
/>
然后我看到了TypeError: Object(...)(...) is not a function
。为什么我不能在渲染方法中使用 HOC,我怎样才能让它工作?顺便说一句Auth(UserProfileView)
,如果放在组件方法中,效果很好。
解决方案
不要在渲染方法中使用 HOC,请参见此处https://reactjs.org/docs/higher-order-components.html。
你可以试试
const UserProfileViewWithAuth = Auth(UserProfileView)
<Route
path="/available-days"
render={props => isAdmin ? <UserProfileViewWithAuth {...props} /> : <Redirect to='/dashboard'/>}
/>
推荐阅读
- java - 音乐循环在 java android 中无法正常工作
- python - 根据相同值出现的次数创建字典键
- mysql - 在 mysql 和 laravel 中加入两个数据库表
- java - 片段是不可见的,但可以点击它的视图
- python - 通过 Python 的 gRPC API 服务器无法工作
- regex - Google表格中的求和正则表达式提取
- javascript - Solidity:“错误:无效的数值”,即使使用引号内的数值,为什么?
- .net - 将“20-001”插入 SQL Server 数据库会导致错误
- python-3.x - 枢轴的熊猫问题:索引包含重复的条目,无法重塑
- php - 哪个更适合 laravel 数据库或日志文件中的用户活动