reactjs - 导航中如何根据 HOC 函数让 react 组件读取或编辑?
问题描述
我想根据授权使组件读取或编辑。例子
const auth = "read" || "edit"
如果 auth 是 read 用户只能允许读取组件。如果 auth 是 edit,用户可以允许编辑组件。
如何将所有组件包装到全局 HOC 函数并基于身份验证如何导航。
导航容器
<Stack.Navigator initialRouteName="LoginComponent">
<Stack.Screen
name="LoginScreen"
component={LoginComponent}
options={{
headerShown: false,
gestureEnabled: false,
}}
/>
<Stack.Screen
name="Upload"
component={Uploadcompoenent}
options={{
headerShown: false,
gestureEnabled: false,
}}
/>
</Stack.Navigator>
(健康)状况
1.让考虑如果用户主管然后用户可以上传新图像。2.如果用户是助理主管,用户只能阅读屏幕。对于助理主管上传选项被禁用。
我想将所有组件放入一个基于 auth 用户可以读取或上传图像的 HOC 函数中。
谢谢,
任何线索将不胜感激:)
解决方案
将组件 <Stack.Navigator/> 视为 HOC,它将呈现几个子元素。
在 Stack.Navigator 组件中,您将拥有可以使用 React.Children.map 以这种方式传递给所有子组件的 auth 值:
export default function ParentComp(props) {
const auth = 'edit';
return React.Children.map(props.children, (child, i) => {
let el = React.cloneElement(child, {
auth
});
return el;
});
}
因此,您的子组件将接受您的身份验证状态作为道具,用于阻止子组件上的某些事件。
export default function ChildComp(props) {
return (
<div>
component is only {props.auth} {props.test}
</div>
);
}
推荐阅读
- java - 未找到带有 maven 和 SQL Server 类的微服务异常
- excel - 如何使用用户输入的文件名导出 excel 文件?
- php - 如何编写查询以从 laravel 中的数据透视表中获取特定数据?
- node.js - 认证后环回 4 错误。键“authentication.currentUser”未绑定到上下文应用程序中的任何值
- go - 实现梯度下降
- azure - Azure 函数运行时异常,系统数据 sqlclient 异常的类型初始化程序,无法加载 DLL 'sni.dll'
- r - RStudio 无法安装“igraph” - C 编译器无法创建可执行文件
- java - 添加 `@SpringBootTest` 注释会触发 IntelliJ 误报
- git - 离开 git 分支的最佳实践
- r - ldply 从系数(模型)中提取标准偏差