zoukankan      html  css  js  c++  java
  • Redux 总结

    1.redux:

    function reducer(state=-,action)

    {

       switch(action.type)

       {

             case '':

               return ...

             case '':

               return ...

             default:

               return ...

        }

    }

    const store = createStore(reducer)

    function listener(){

          const current = store.getState()

          console.log(current);

    }

    store.subscribe(listener)

    store.dispatch({type:'...'})

    2.redux配合react一起使用:

    index.js

    function render()

    {

        ReactDom.render(<App store={store}/>,document.getElementById('root'))

    }

    store.subscribe(render)

    App.js: 里面是组件和store

    index.reducer.js 里面是reducer 和 actioncreator

    3.使用redux-thunk让redux处理异步:

    一.安装redux-thunk

    二.使用applyMiddleware开启thunk中间件

    三.Action可以返回函数,使用dispatch提交action

    首先开启中间件:

    引入thunk:

    然后创建store

    调试工具:

    4.使用react-redux:

    npm install react-redux –save

    忘记subscribe,只使用reducer、action、和dispatch

    ·Provier组件在应用最外层,传入store即可,只用一次

    ·Connect负责从外部获取组建需要的参数

    index.js:

    App.js(这是一个组件)

    *用装饰器的方式书写:

  • 相关阅读:
    11组 团队展示
    11组Alpha冲刺4/6
    11组Alpha冲刺2/6
    11组Alpha冲刺3/6
    EF code first 分页显示
    多条件分页存储过程控制器写法
    UML的9种图
    C#设计模式(2)——简单工厂模式
    多条件分页存储过程PageCommon写法
    五分钟读懂UML类图
  • 原文地址:https://www.cnblogs.com/eret9616/p/9261033.html
Copyright © 2011-2022 走看看