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(这是一个组件)

    *用装饰器的方式书写:

  • 相关阅读:
    (十七)物联网之 WIFI 一键配网 smartConfig 浅析
    mac 下设置jdk 路径,设置hadoop 路径
    mac 下配置 git
    mysql mac  安装修改初始密码
    linux 下安装 mysql5.7.16安装
    linux 安装nexus
    linux 安装maven
    linux 下安装mongodb
    转发和重定向
    spring mvc controller间跳转 重定向 传参
  • 原文地址:https://www.cnblogs.com/eret9616/p/9261033.html
Copyright © 2011-2022 走看看