Redux重要API

重要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 标签

  
    
  , 

connect 函数

function mapStateToProps(state){
  return {
    n: state.n
  }
}
function mapDispatchToProps(dispatch) {
  return {
    add1: ()=> dispatch({type:'add', payload: 1})
  }
}

export default connect(mapStateToProps,mapDispatchToProps)(App);

你可能感兴趣的:(Redux重要API)