Javascript 如何解决与提供商my store链接并将数据传递到我的Redux React应用程序中的组件的问题?
我得到了这个错误:Javascript 如何解决与提供商my store链接并将数据传递到我的Redux React应用程序中的组件的问题?,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我得到了这个错误: Uncaught Error: Could not find "store" in either the context or props of "Connect(WebShop)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(WebShop)". My store.js是: 我在我的webshop.js中使用c
Uncaught Error: Could not find "store" in either the context or props of "Connect(WebShop)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(WebShop)".
My store.js是:
我在我的webshop.js中使用connect,其中我使用:
const mapDispatchToProps = (dispatch) => {
return {
onCartAdd: (cart) => {
dispatch(addCart(cart));
},
}
}
export default connect(null, mapDispatchToProps)(WebShop);
如果需要更多的代码来找到解决方案,请告诉我。现在,我不确定这个错误是来自webshop.js文件还是来自组件和商店之间的链接。为什么会出现此错误以及如何修复它?替换路由器和提供商的顺序
ReactDOM.render(
<Provider store={store}>
<Router history={ hashHistory }>
<Route path='/' component={ App }>
<IndexRoute component={ Home } />
<Route path='Webshop' component={ Webshop } />
<Route path='Cart' component={ Cart } />
</Route>
</Router>,
</Provider>
document.getElementById('app') // eslint-disable-line
);
首先,您的提供程序需要位于路由的最外面。您可能还需要将路由封装在一个函数中,例如{=>}。我觉得您的代码很好。通过在ReactDOM.render之前添加console.logstore行来检查是否正确地传递了store。我得到了对象{dispatch:function,subscribe:function,getState:function,replaceReducer:function,Symbolobservable:function}@TharakaWijebandaraI以前尝试过这个,但出于某种原因它给了我一个错误..但现在它很好..谢谢。
const mapDispatchToProps = (dispatch) => {
return {
onCartAdd: (cart) => {
dispatch(addCart(cart));
},
}
}
export default connect(null, mapDispatchToProps)(WebShop);
ReactDOM.render(
<Provider store={store}>
<Router history={ hashHistory }>
<Route path='/' component={ App }>
<IndexRoute component={ Home } />
<Route path='Webshop' component={ Webshop } />
<Route path='Cart' component={ Cart } />
</Route>
</Router>,
</Provider>
document.getElementById('app') // eslint-disable-line
);