Redux重要API

2019-01-08  本文已影响0人  我也不知道啊丶

重要API

Reducer 函数

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);
上一篇下一篇

猜你喜欢

热点阅读