React + Redux:不变违规:在“Connect(Body)”的上下文或道具中都找不到“store”
React + Redux : Invariant Violation: Could not find "store" in either the context or props of "Connect(Body)"
我正在尝试使用 react
+ redux
+ react-redux
+ react-router
理念将我的组件连接到商店。即使父级被商店封装,我也会收到此错误:
[Invariant Violation: Could not find "store" in either the context or props of "Connect(Body)".
Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Body)".]
name: 'Invariant Violation', framesToPop: 1 }
代码(为简洁起见,导入缩短)
组件/表.jsx
export default class Table extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
{this.props.barcodes.map(barcode =>
<p key={barcode.value}>
)}
</tbody>
</table>
);
}
}
containers/BarcodeListing.jsx
import Table from 'components/Table'
import { connect } from 'react-redux'
function mapStateToProps(state) {
return { barcodes: state.barcodes };
}
@connect(mapStateToProps)
class Body extends React.Component {
render() {
return (
<Table barcodes={barcodes}/>
);
}
}
export default class extends React.Component {
render() {
return (
<Container>
<Body />
</Container>
);
}
}
routes.jsx
import BarcodeListing from 'containers/barcodeListing';
const reducer = combineReducers(Object.assign({}, reducers, {
routing: routeReducer
}));
const finalCreateStore = compose(window.devToolsExtension())(createStore);
const store = finalCreateStore(reducer, {});
return (
<Provider store={store}>
<Router history={BrowserHistory} onUpdate={onUpdate}>
<Route path='/' component={BarcodeListing} />
</Router>
</Provider>
);
};
包.json
"react": "shripadk/react-ssr-temp-fix",
"react-dom": "^0.14.6",
"react-hot-loader": "^1.2.7",
"react-redux": "^4.0.6",
"react-router": "^1.0.0-beta3",
"react-style": "^0.5.5",
"redux": "^3.0.6",
"redux-logger": "^2.3.2",
"redux-simple-router": "^1.0.2",
看来你的 React 是0.13.3
版本,反应<0.14
存在问题。参见Dan Abramov(redux作者)的回答:
在 Router 1.0 上它不会以这种方式工作,直到 React 0.14 出来。
顶级路由处理程序 (App) 不是较低级别处理程序 (DashboardApp) 的所有者,因此上下文不会传播。这将在 React 0.14 中修复。
这意味着您的store
不会从Provider
传给Body
......
如果你不能改变 React 的版本,试着将 store 作为道具传递给Body
组件。
像这样:
import { store } from '../routes.jsx';
...
return (
<Container>
<Body store={ store } />
</Container>
);
相关文章:
- 将函数的上下文应用于javascript变量
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 使用JQuery的动态上下文菜单
- 如何访问UIWebView'的子窗口上下文
- TypeError:_this.store.getState在使用来自Redux的连接时不是函数
- JQuery在单击正文时隐藏上下文菜单
- Query JS Ext.data.Store
- JQuery上下文菜单显示/隐藏问题
- 如何从HTTP上下文对象中获取Post数据
- HTML字符串作为上下文
- 为什么我的上下文选择器和.buttonset()在ie中花费了这么长时间
- 丢失对象“;这个“;方法中的上下文
- 防止在移动Safari(iPad/iPhone)中长按/长按默认上下文菜单
- d3防止在上下文菜单上触发mouseout
- 是否可以在不更改上下文的情况下调用函数.apply
- 调用$.each()函数时上下文发生变化
- 何时可以;我的用户脚本在Javascript中触发右键单击(上下文菜单)
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 回流:让多个组件访问同一个Store
- React + Redux:不变违规:在“Connect(Body)”的上下文或道具中都找不到“store”