刚学redux, 不是很明白一下两个function的参数state和dispatch是从哪里来的?求明白人给讲讲!!!
function mapStateToProps(state){
return {
counter: state.counter
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators(actions, dispatch)
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
mapStateToProps(state)中的初始state, 可以在创建store的时候指定一个initialState, 代码:
也可以不指定initialState. redux的container组件在执行connect()方法的时候, 会默认调用一个type="@@redux/INIT"的action, 这样的话可以直接在reducer中处理初始state为空的情况, 代码:
注意: 在使用第一种方法传入initialState时, 如果使用的是Immutable, 默认情况下会报错:The initialState argument passed to createStore has unexpected type of "Object". 修改的办法是安装redux-immutable, 使用redux-immutable创建reducer, 代码:
木有人回答。。。
connect 传过来的