在App.js中的react-native app中,我使用react-native-router-flux并连接每个场景以使用“RouterWithRedux”存储redux,如下所示:
export default class App extends Component {
render() {
const RouterWithRedux=connect()(Router);
return (
<Provider store={store}>
<RouterWithRedux>
<Scene key="root">
<Scene key="landing" component={Landing} title="Landing" initial={true}/>
<Scene key="pageTwo" component={PageTwo} title="PageTwo" />
<Scene
key="rootTabBar"
tabs={true}
tabBarStyle={{backgroundColor: '#ffffff'}}>
<Scene key="home" component={Home} title="Home" icon={TabIcon} initial />
<Scene key="search" component={Search} title="Search" icon={TabIcon} />
</Scene>
</Scene>
</RouterWithRedux>
</Provider>
)
}
}
export default App;
我想使用连接也app到redux在app.js中使用“mapStateToProps”和“mapDispatchToProps”。我怎么用这个?我试试
export default connect(mapStateToProps, mapDispatchToProps)(App);
但我遇到了错误,说:
Unhandled JS Exception: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(App)".
最后我发现“connect()”函数只能在标签内部的组件中使用,因此我们不能在App.js中使用它,因为我们正在创建商店并将其提供给App.js的孩子。
话虽这么说,我们不需要使用mapStateToProps只使用store.getState()和store.dispatch()来访问状态。