在 react-redux 项目中使用 connect 时在哪里初始化 store

Where to initialize store when using connect in react-redux project

我是 React 和 redux 的新手,所以很困惑在哪里用虚拟数据初始化存储?我在根组件中使用提供程序。 我发现访问商店的唯一两种方法是使用此方法:-

Provider.childContextTypes = {
   store: React.PropTypes.object
}

或通过使用连接,

const mapStateToProps = state => ({
 state:state.contacts
});

const mapDispatchToProps = dispatch => ({
 setDummyData: () => dispatch(setDummyData())
});

export default connect(
 mapStateToProps,
 mapDispatchToProps
)(ComponentName)

但联系人未初始化.. 如何接近?

任何可以找到连接和虚拟数据示例的链接都将非常有用

createStore 中对整个存储或在每个切片的 reducer 中进行。

https://redux.js.org/api/createstore

您可以在 index.js 文件中初始化 store(例如,如果您使用 create-react-app)。初始化商店后,您可以将其传递给您的提供商并使用提供商包装您的应用程序:

const = createStore(someReducers, someMiddleWareIfYouHaveIt)
<Provider store={store}>
    <App />
</Provider>

ReactJS 网站上也有一些初学者工具包 here