react-native - 如何在 routeConfig 中访问 redux 存储(react-navigation)
问题描述
这是代码示例:
const mapStateToProps = (state: RootState) => ({
isSwipeEnabled: state.permissions.isSwipeEnabled,
});
const MainNavigator = createMaterialTopTabNavigator(
{
Screen1,
Screen2: {
screen: Screen2,
navigationOptions: {
swipeEnabled: isSwipeEnabled, <==HERE I WANT TO USE MY REDUX STATE
},
},
},
{
initialRouteName: 'Screen1',
tabBarPosition: 'bottom',
},
);
export default connect(mapStateToProps)(MainNavigator);
是否可以在 createMaterialTopTabNavigator 函数中访问 redux?如果是,怎么做?
谢谢!
解决方案
您可以在连接到 redux 的 App 组件的构造函数中创建导航器。在构造函数中,您可以从道具访问映射状态:
const mapStateToProps = (state: RootState) => ({
isSwipeEnabled: state.permissions.isSwipeEnabled
});
class NavigationApp extends React.Component {
constructor(props) {
super(props);
this.mainNavigator = createMaterialTopTabNavigator(
{
Screen1,
Screen2: {
screen: Screen2,
navigationOptions: {
// here you now can use your mapped redux state from props
swipeEnabled: props.isSwipeEnabled,
}
}
},
{
initialRouteName: 'Screen1',
tabBarPosition: 'bottom'
},
);
}
render() {
// use here any function from react-navigation which you are using in your environment
// e.g. createAppContainer
// in this example I am using createBrowserApp for using react-navigation in web app
const App = createBrowserApp(this.mainNavigator);
return (<App />);
}
}
export default connect(mapStateToProps)(NavigationApp);
推荐阅读
- python - 如何限制 django 数据库中的条目数?使用默认数据库(sqlite3)
- r - Is there any r code that gives an output of a discreate line graph?
- php - 如何显示 laravel 404 页面?
- typescript - Ionic 4:NFC 无法导入 NFC 和 Ndef
- keras - 如果我想预测 0-1 间隔的连续结果,我应该使用哪个输出激活和损失?
- c++ - 如何找到双向链表的最大元素?
- cmake - OpenMP not building/linking in macOS Catalina?
- git - 这个命令做了什么“ git checkout -- 。”
- python - 如何在手机上实现发送otp?
- react-native - 将特定元素添加到地图