初次接觸redux從而在網絡上找學習資源。
對react+redux教程(一) 文章中的state
在什么位置定義,為什么命名為counter
.
下面是事故代碼:
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Counter from '../components/Counter'
import * as CounterActions from '../actions/counter'
//將state.counter綁定到props的counter
function mapStateToProps(state) {
return {
counter: state.counter
}
}
//將action的所有方法綁定到props上
function mapDispatchToProps(dispatch) {
return bindActionCreators(CounterActions, dispatch)
}
//通過react-redux提供的connect方法將我們需要的state中的數據和actions中的方法綁定到props上
export default connect(mapStateToProps, mapDispatchToProps)(Counter)
疑惑點在
function mapStateToProps(state) {
return {
counter: state.counter
}
}
(以上代碼段是將redux中的state
中的counter
通過connetct
方法綁定到Counter
上)
如何知道state
中有counter
,在什么地方進行了定義?
分析:counter:state:counter
- 第一個
counter
是component
中的值名,在component
中需要根據這個名字來獲得。(這個命名你可以隨意) - 第二個
counter
(即state中的counter),是在combineReducers
中給定的:
Paste_Image.png