我收到此错误,我用谷歌搜索了解决方案,但似乎一切都应该正确设置。
完全错误是:"不变冲突:在"连接(应用组件("的上下文中找不到"存储"。要么将根组件包装在 中,要么在连接选项中将自定义 React 上下文提供程序传递给 Connect(AppComponent(。
此错误位于:在 Connect(AppComponent( (at renderApplication.js:34( in RCTView (at View.js:45( in View (at AppContainer.js:98( in RCTView (at View.js:45( in View (at AppContainer.js:115( in AppContainer (at renderApplication.js:33(
这是我的代码:
索引.js
import { Provider } from 'react-redux'
import { createStore } from 'redux';
import App from './app/App';
import appState from './app/redux/reducers';
import {name as appName} from './app.json';
let store = createStore(appState);
export default class AppRoot extends Component {
render() {
return (
<Provider store={store}>
<App/>
</Provider>
);
}
}
AppRegistry.registerComponent(appName, () => App);
应用.js
//import...
class AppComponent extends Component {
//code..
}
const mapStateToProps = (state, props) => {
return {}
};
const mapDispatchToProps = (dispatch, props) => {
return {
onFavoriteChange: (id, type) => {
switch(type){
//cases...
}
}
}
};
const App = connect(
mapStateToProps,
mapDispatchToProps
)(AppComponent);
export default App;
索引.js(在/reducers 中(
import { combineReducers } from 'redux';
//imports...
let appState = combineReducers({
//all import...
});
export default appState;
在根目录中注册的App
组件不是AppRoot
。这意味着 React 渲染App
没有提供存储Provider
。然后,connect
HOC 引发错误。您必须将呼叫更新为:
AppRegistry.registerComponent(appName, () => AppRoot);