求和案例(redux完整版)(异步action)

action是一个普通js对象,包含type和data两个属性。其中,type属性,必选;data属性,可选。

action本身并无“同步”或“异步”之说,“同步action”、“异步action”更多的只是区分:

  1. action creator的返回值是一个普通的js对象,即所谓的“同步action”。
  2. action creator的返回值是一个函数,即所谓的“异步action”。

本篇将使用异步action,涉及的文件有:

  1. redux/constant.js,无变更
  2. redux/reducer.js,无变更
  3. redux/action.js,有变更
  4. redux/store.js,有变更
  5. components/Counter/index.jsx,有变更

要实现异步action,注意以下几点:

  1. 安装redux-thunknpm install --save redux-thunk
  2. 将redux-thunk配置在store中
import {createStore,applyMiddleware} from "redux";
import thunk from "redux-thunk";
import reducer from "./reducer.js";
export default createStore(reducer,applyMiddleware(thunk));
  1. 定义异步action
    异步action返回的函数,接受的第一个参数是dispatch(即store.dispatch)。
export const createIncrementAsyncAction = (data,time) => {
    return (dispatch) => {
        setTimeout(() => {
            dispatch(createIncrementAction(data));
        },time)
    }
}
  1. 派发异步action
store.dispatch(createIncrementAsyncAction(Number(value),1000))

redux/constant.js

export const INCREMENT = 'increment';
export const DECREMENT = 'decrement';

redux/reducer.js

import { INCREMENT,DECREMENT } from "./constant";

const initState = 0

export default function reducer(preState=initState,action){
    const {type,data} = action;

    switch(type){
        case INCREMENT: return preState+Number(data);
        case DECREMENT: return preState-Number(data);
        default: return preState
    }
}

redux/action.js

import { INCREMENT,DECREMENT } from "./constant"

export const createIncrementAction = data => ({type:INCREMENT,data});
export const createDecrementAction = data => ({type:DECREMENT,data});

export const createIncrementAsyncAction = (data,time) => {
    return (dispatch) => {
        setTimeout(() => {
            dispatch(createIncrementAction(data));
        },time)
    }
}

redux/store.js

import { createStore,applyMiddleware} from "redux";
import thunk from "redux-thunk";
import counterReducer from "./reducer.js";

export default createStore(counterReducer,applyMiddleware(thunk));

Counter/index.jsx(Counter组件)

import React, { Component } from 'react'
import store from "../../redux/store.js";
import { createIncrementAction,createDecrementAction, createIncrementAsyncAction } from '../../redux/action.js';

export default class Counter extends Component {

    componentDidMount(){
        store.subscribe(() => {
            this.setState({})
        })
    }

    increment = () => {
        const {value} = this.selectEl;
        store.dispatch(createIncrementAction(Number(value)));
        
    }
    decrement = () => {
        const {value} = this.selectEl;
        store.dispatch(createDecrementAction(Number(value)));

    }
    incrementOdd = () => {
        const {value} = this.selectEl;
        const count = store.getState();
        if(count % 2 !== 0){
            store.dispatch(createIncrementAction(Number(value)));
        }
    }
    incrementWait = () => {
        const {value} = this.selectEl;  
        store.dispatch(createIncrementAsyncAction(Number(value),1000))
    }

  render() {
    const {increment,decrement,incrementOdd,incrementWait} = this;
    return (
        <div>
            <h2>当前求和为{store.getState()}</h2>
            <select ref={c => this.selectEl = c}>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>&nbsp;
            <button onClick={increment}>+</button>&nbsp;
            <button onClick={decrement}>-</button>&nbsp;
            <button onClick={incrementOdd}>和为奇数时加</button>&nbsp;
            <button onClick={incrementWait}>等一等再加</button>
        </div>
    )
  }
}

相关链接

求和案例(纯react版本)
求和案例(redux精简版)
求和案例(redux完整版)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值