reactjs - 使用高阶组件传递属性
问题描述
我对如何将位置对象传递给我的子函数感到困惑。它永远不会手动传入,并且似乎是自动传入的。
这可能是对到达路由器的增强吗?
我正在使用盖茨比。
高阶组件:
const PrivateRoute = ({ component: Component, location, ...rest }) => {
return (
<div>
<h5>{location.pathname}</h5>
<Component {...rest} />
</div>
)
}
使用地点:
<Layout>
<Router>
<PrivateRoute path="/app/authorize" component={authContent} />
</Router>
</Layout>
)
子组件:
const authContent = ({ ...rest }) => (
<div>
<h1> {rest.name}</h1>
<h1>Hello World</h1>
</div>
)
解决方案
实际上有 props 传递到路由的子组件中:
启用路由的历史记录。
匹配访问 URL 参数
和位置访问当前位置。
HOC 只是将这些作为附加道具添加到其渲染函数中。
希望有帮助。快乐编码。
推荐阅读
- php - 刷新页面时出现警报
- angular - 子组件中的 ExpressionChangedAfterItHasBeenCheckedError
- python - 在 Django 中导入自定义类后输入错误
- java - Java Jersey 2:独立?
- c# - C# GUI 键盘快捷键
- python - TypeError:数据类型不理解,numpy.zeros
- django - 上传到 Django 中的 FileField 时保留文件名
- git - 将 Git 存储库从 GitHub 分叉到 GitLab
- c++ - 如何挂钩延迟导入
- maven - maven 依赖项:copy-dependencies 错误地重命名了 .so 依赖项