重要API
const reducer = (state, action)=>{
if(state === undefined){
return {n: 0}
}else{
if(action.type === 'add'){
var newState = {n: state.n + action.payload}
return newState
}else{
return state
}
}
}
store
const store = createStore(reducer)
store.subscribe
store.subscribe(()=>{
render()
})
store.dispatch
store.dispatch({type:'add', payload: 1})
React-Redux
Provider 标签
<Provider store={store}>
<App />
</Provider>,
connect 函数
function mapStateToProps(state){
return {
n: state.n
}
}
function mapDispatchToProps(dispatch) {
return {
add1: ()=> dispatch({type:'add', payload: 1})
}
}
export default connect(mapStateToProps,mapDispatchToProps)(App);
网友评论