美文网首页
Redux重要API

Redux重要API

作者: 我也不知道啊丶 | 来源:发表于2019-01-08 16:59 被阅读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);
    

    相关文章

      网友评论

          本文标题:Redux重要API

          本文链接:https://www.haomeiwen.com/subject/kixbrqtx.html