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

问题描述 投票:0回答:2

我是新来的反应者和redux者,所以很困惑在哪里用伪数据初始化存储? 我在根组件中使用provider。 访问我发现的商店的唯一两种方法是使用以下方法:-

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

或使用connect,

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

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

export default connect(
 mapStateToProps,
 mapDispatchToProps
)(ComponentName)

但是联系人尚未初始化..如何处理?

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

reactjs redux react-redux store
2个回答
0
投票

在整个商店的createStore中或每个切片的reducer中进行。

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


0
投票

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

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

有一些入门套件这里的ReactJS网站上为好。

© www.soinside.com 2019 - 2024. All rights reserved.