Redux动作已触发,但无法导航(React-Navigation)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redux动作已触发,但无法导航(React-Navigation)相关的知识,希望对你有一定的参考价值。
我刚开始一个新项目,我目前正在将Redux-Saga与React-Navigation集成。这是我使用的版本:
"react": "16.6.1",
"react-native": "0.57.5",
"react-navigation": "^1.5.11",
"react-navigation-redux-helpers": "^1.0.5",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-saga": "^1.0.1",
"reduxsauce": "^1.0.1",
这是我的Main文件和Navigator文件:
export default class Root extends React.Component {
props: Props;
renderApp() {
const store = createStore(rootReducer, applyMiddleware(logger, middleware));
return (
<Provider store={store}>
<Navigator/>
</Provider>
);
}
render() {
return this.renderApp();
}
}
AppRegistry.registerComponent('Root', () => Root);
/*************************/
export const Navigator = new StackNavigator({
Main: {
screen: MainScreen
},
Tutorial:{
screen: TutorialScreen
}
},
{
initialRouteName: 'Main',
mode:'modal'
});
export const middleware = createReactNavigationReduxMiddleware(
"root",
state => state.navigation,
);
const addListener = createReduxBoundAddListener("root");
class Nav extends React.Component {
render() {
return (
<Navigator
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.navigation,
addListener
})} />
)
}
}
const mapStateToProps = (navigation) => ({
navigation,
});
const mapDispatchToProps = dispatch => ({
dispatch
});
export default connect(mapStateToProps, mapDispatchToProps)(Nav)
当我在主文件上为Button分配导航操作navigate: (routeName, params = {}) => dispatch(Actions.navigate(routeName, params))
时,正确调用了Actions。
但是视图没有“导航”并且仍然停留在第一个视图上。看起来redux已正确设置,但导航操作管理不善。但是我无法找到这个问题的确切原因。它可能来自哪里?
我的容器文件:
type Props = {
user?: Object,
navigate: (routeName: string, params?: Object) => void,
};
class MainScreen extends React.Component {
render() {
const {user, navigate}= this.props;
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Button title="mainScreen" onPress={()=> navigate("Tutorial",{})} >
</Button>
</View>
);
}
}
const mapDispatchToProps = dispatch => ({
navigate: (routeName, params = {}) => dispatch(Actions.navigate(routeName, params))
});
const mapStateToProps = (state) => {
const { user } = state
return { user }
};
export default connect(mapStateToProps, mapDispatchToProps)(MainScreen);
答案
每次渲染组件时,您都在重新创建商店。
const store = createStore(rootReducer, applyMiddleware(logger, middleware));
export default class Root extends React.Component {
props: Props;
renderApp() {
return (
<Provider store={store}>
<Navigator/>
</Provider>
);
}
render() {
return this.renderApp();
}
}
以上是关于Redux动作已触发,但无法导航(React-Navigation)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 react-redux 调试 action.type 问题,单动作类型单减速器,但无法识别类型
Redux&React路由器:梳理调度和导航(history.push)