美文网首页我爱编程
【React.js 08】Redux处理异步事件

【React.js 08】Redux处理异步事件

作者: IUVO | 来源:发表于2018-04-01 14:09 被阅读618次

基于上一篇【React.js 07】React中使用Redux所举的例子,我们对于派发的动作处理全部是同步的,这一篇我们将实现异步的。

例子依旧使用上一篇文章中所说的例子,代码有对应的贴在上面了。


上一篇的例子中,我们对于派发的动作处理全部是同步的,但是实际项目中,往往都是异步的动作居多,所以我们需要让Redux能够处理异步操作。

Redux处理异步,需要结合中间件,这里我们使用redux-thunk插件去处理。
1、安装redux-thunk插件

npm install redux-thunk --save

2、因为Redux默认只处理同步事件,异步事件需要redux-thunk的中间件,所以我们要使用applyMiddleware去开启thunk的中间件。
我们到index.js文件中去开启thunk的中间件。
首先引入管理中间件的函数applyMiddleware()

import { createStore, applyMiddleware} from 'redux'

然后,把函数applyMiddleware()作为参数之一传给createStore方法:

//所以首先还是需要再引入thunk对象
import  thunk  from 'redux-thunk'

//然后把thunk给applyMiddleware(),applyMiddleware再给createStore(),这样就开启了中间件
const store = createStore(manageCompany, applyMiddleware(thunk))

开启了中间件后,接下来,我们就能执行异步操作了。

3、还记得上一篇中的action creator吗,我们利用它返回action type对象,而开启插件后,就可以直接返回函数,在这个函数里面,我们在异步完成后,再去使用dispatch派发事件。
index.redux.js文件中去创建并export一个异步的action creator了:

// type为HIRE的异步action
export function hireAsync() {
  return dispatch=>{
    setTimeout(()=>{
      dispatch(hire())
    },2000)
  }
}

reducer中新增了异步事件,那么外部就要能够接收到该事件,并且将其以属性的方式传递给组件App

//import 新的异步方法
import { manageCompany, hire ,fire ,hireAsync} from './index.redux'
//render中把新增了异步事件以属性的方式传递给组件`App`
{...
  ReactDom.render(
    <App store={store} hire={hire()} fire={fire()} hireAsync={hireAsync()}/>,
    document.getElementById('root')
  )
...}

然后App组件中加入对应事件触发:

<p onClick=<p onClick={this.handlerCompanyManagement.bind(this,this.props.hireAsync)}>雇佣一人,晚两天到</p>

这样,就完成了一个异步的处理事件。

上代码!!!!

./src/App.js

import React , { Component } from 'react'

class App extends Component {
  handlerCompanyManagement(action) {
    this.props.store.dispatch(action)
  }

  render() {
    const store = this.props.store
    return (
      <div>
        <h1>公司现在有{store.getState()}人</h1>
        <p onClick={this.handlerCompanyManagement.bind(this,this.props.hire)}>雇佣一人</p>
        <p onClick={this.handlerCompanyManagement.bind(this,this.props.hireAsync)}>雇佣一人,晚两天到</p>
        <p onClick={this.handlerCompanyManagement.bind(this,this.props.fire)}>开除一人</p>
      </div>
    )
  }
}

export default App


./src/index.js
import React from 'react'
import ReactDom from 'react-dom'
import App from './App'
import { createStore ,applyMiddleware} from 'redux'
import thunk from 'redux-thunk'
import { manageCompany, hire ,fire ,hireAsync} from './index.redux'

// 通过reducer创建store
const store = createStore(manageCompany, applyMiddleware(thunk))

// 定义一个render()函数去渲染页面
function render() {
  ReactDom.render(
    <App store={store} hire={hire()} fire={fire()} hireAsync={hireAsync()}/>,
    document.getElementById('root')
  )
}

// 先手动调用一次看看页面效果
render()

// 然后再把render方法传入subscribe作为监听变化时自动刷新调用的方法
store.subscribe(render)


./src/index.redux.js
// 定义成常量,尽量避免使用魔法值
const HIRE = 'hireEmployee'
const FIRE = 'fireEmployee'

// 导出并且定义reducer
export function manageCompany(total = 1, action) {
  switch (action.type) {
    case HIRE:
      return total + 1;
    case FIRE:
      return total - 1;
    default:
      return 1;
  }
}

// type为HIRE的action
export function hire() {
  return {type : HIRE}
}
// type为HIRE的异步action
export function hireAsync() {
  return dispatch=>{
    setTimeout(()=>{
      dispatch(hire())
    },2000)
  }
}
// type为FIRE的action
export function fire() {
  return {type : FIRE}
}

Done!

相关文章

  • 【React.js 08】Redux处理异步事件

    基于上一篇【React.js 07】React中使用Redux所举的例子,我们对于派发的动作处理全部是同步的,这一...

  • redux

    使用步骤 redux只处理同步 redux处理异步请求中间件· 在引入redux的时候引入applyMiddlew...

  • React解惑之 redux-saga

    Redux-saga 概述 redux-saga和redux-thunk一样,是用于处理redux应用异步请求的中...

  • redux-thunk

    一、redux-thunk介绍redux-thunk用于处理异步action,同类型的有redux-saga 二、...

  • Redux处理异步的中间件redux-saga

    有关于异步处理,通用解决方案有这些: redux-thunk,redux-promise,redux-saga最后...

  • redux

    一、同步数据流动 二、异步数据流动 说明:应用 middleware 后 redux 处理事件的逻辑,每一个 mi...

  • 【React.js 10】React自动连接Redux

    基于【React.js 07】React中使用Redux所举的例子(后面两篇08、09的文章案例有所修改,也都贴上...

  • React-依赖

    create-react-app 初始化空的react项目 redux-saga 处理异步利器 redux-seq...

  • redux-thunk

    redux-thunk 是一款redux中间件 工作流程 目前是先处理异步在派发动作 使用 redux-thunk...

  • 初识redux-saga

    最近项目用了dva,dva对于异步action的处理是用了redux-saga,故简单学习了下redux-saga...

网友评论

    本文标题:【React.js 08】Redux处理异步事件

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