Passing Redux store in props

房东的猫 提交于 2019-12-05 09:18:44

Don't pass the store instance to <App>. That does nothing, and React-Redux v6 is warning you about that.

In React-Redux v5, passing the store directly as a prop to a connected component allowed, and there were rare occasions when it was useful, but in v6 that has been removed.

Please see my post Idiomatic Redux: The History and Implementation of React-Redux for some details on why that part of the API was removed.

Also, note that returning the entire Redux state object from a mapState is usually not a good idea, and if by some reason you do actually want to do that, you don't need to spread it - just return state. See the React-Redux docs section on writing mapState functions for some explanations.

React Redux 7.0.1 allows passing store as a Prop again.

From the release notes:

We've brought back the ability to pass a store as a prop directly to connected components. This was removed in version 6 due to internal implementation changes (components no longer subscribed to the store directly). Some users expressed concerns that working with context in unit tests was not sufficient. Since our components use direct subscriptions again, we've reimplemented this option, and that should resolve those concerns.

The problem lies on line 25 in ReduxProvider i.e.

<App store={this.store}>

This component is wrapped with Provider (which can and should accept the store as a prop) and the purpose of this is to make the store available to the rest of the application - so passing the store directly into child components isn't necessary.

To get access to the store, use the connect function to connect the component to the store and then mapStateToProps / mapDispatchToProps to access the state and/or dispatch actions.

More info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!