-
Notifications
You must be signed in to change notification settings - Fork 0
Experience Learning In React Redux
janeluck edited this page Oct 30, 2018
·
1 revision
-
connect
mapStateToProps mapDispatchToProps
-
Provider
创建整体上下文, 提供context里面的store
-
shallowEqual
-
selectors reselector
-
Provider提供context的store
-
Connect返回高阶组件, 组件包裹在一个随着store改变而触发state改变的connect组件中
// 思路简化版伪代码
class Connect extends React.Component {
construtor(){
super()
this.state = {
allProps: {
}
}
}
// 考虑到ssr,订阅放在了DidMount
componentDidMount(){
const { store } = this.context
this._updateProps()
store.subscribe(() => this._updateProps())
}
_updateProps () {
const { store } = this.context
let stateProps = mapStateToProps
? mapStateToProps(store.getState(), this.props)
: {} // 防止 mapStateToProps 没有传入
let dispatchProps = mapDispatchToProps
? mapDispatchToProps(store.dispatch, this.props)
: {} // 防止 mapDispatchToProps 没有传入
this.setState({
allProps: {
...stateProps,
...dispatchProps,
...this.props
}
})
}
render () {
return <WrappedComponent {...this.state.allProps} />
}
}
Welcome to the AwesomeNote wiki!
-
JavaScript
-
React
-
Node
-
Electron
-
计科基础
-
数据结构与算法
-
Database
-
Git
-
Unix
-
Immutable
-
Lodash
-
Browser
-
Http
-
离散数学