42_redux_counter应用_redux异步版本

Posted zhanzhuang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了42_redux_counter应用_redux异步版本相关的知识,希望对你有一定的参考价值。

前言:

  redux默认不支持异步编程,需要下载redux插件(异步中间件)

  如何下载:

    npm install --save redux-thunk

项目结构:

技术图片

代码:

技术图片
import React, {Component} from ‘react‘
import PropTypes from ‘prop-types‘

export default class Counter extends Component {
    static propTypes = {
        count: PropTypes.number.isRequired,
        increment: PropTypes.func.isRequired,
        decrement: PropTypes.func.isRequired,
        incrementAsync: PropTypes.func.isRequired
    }

    increment = () => {
        //1.得到选择的增加数量
        const number = this.select.value * 1
        //2.调用store的方法更新状态
        this.props.increment(number)
    };

    decrement = () => {
        //1.得到选择的增加数量
        const number = this.select.value * 1
        //2.调用store的方法更新状态
        this.props.decrement(number)
    };

    incrementIfOdd = () => {
        //1.得到选择的增加数量
        const number = this.select.value * 1
        //2.得到原本的count状态
        const count = this.props.count
        //3.判断,满足条件再更新状态
        if (count % 2 === 1) {
            //调用store方法更新状态
            this.props.increment(number)
        }
    }

    incrementAsync = () => {
        //1.得到选择的增加数量
        const number = this.select.value * 1

        this.props.incrementAsync(number)
    };

    render() {
        const {count} = this.props
        // debugger
        return (
            <div>
                <p>click {count} times</p>
                <div>
                    <select ref={select => this.select = select}>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>&nbsp;
                    <button onClick={this.increment}>+</button>
                    &nbsp;
                    <button onClick={this.decrement}>-</button>
                    &nbsp;
                    <button onClick={this.incrementIfOdd}>increment odd</button>
                    &nbsp;
                    <button onClick={this.incrementAsync}>increment async</button>
                    &nbsp;
                </div>
            </div>
        )
    }
}
counter.jsx
技术图片
import React from ‘react‘
import {connect} from "react-redux";

import {decrement, increment, incrementAsync} from "../redux/actions";
import Counter from ‘../components/counter‘

export default connect(
    state => ({count: state}),
    {increment, decrement, incrementAsync}
)(Counter)
app.jsx
技术图片
/*
* 包含所有action type的常量字符串
* */

export const INCREMENT = ‘INCREMENT‘;
export const DECREMENT = ‘DECREMENT‘;
action-types.js
技术图片
import {INCREMENT, DECREMENT} from ‘../redux/action-types‘;
/*
* 包含所有action creator
* 同步的action都是返回一个对象
* 异步的action返回的是一个函数
* */

//增加
export const increment = (number) => ({
    type: INCREMENT, data: number
})
//减少
export const decrement = (number) => ({
    type: DECREMENT, data: number
})
//异步action
export const incrementAsync = (number) => {
    return dispatch => {
        //异步的代码
        setTimeout(() => {
            //1S之后才去分发一个增加的action
            dispatch(increment(number))
        }, 1000)
    }
}
actions.js
技术图片
/*
* 包含n个reducer函数的模块
* */
export function counter(state = 0, action) {

    console.log(‘counter()‘, state, action)

    switch (action.type) {
        case ‘INCREMENT‘:
            return state + action.data
        case ‘DECREMENT‘:
            return state - action.data
        default:
            return state
    }

}
reducers.jsx
技术图片
import {createStore, applyMiddleware} from ‘redux‘;
import thunk from ‘redux-thunk‘

import {counter} from ‘./reducers‘;

//生成store对象
const store = createStore(
    counter,
    applyMiddleware(thunk)//应用上异步中间件
);//内部会第一次调用reduer函数得到初始state
console.log(store, store.getState());

export default store
store.js
技术图片
import React from ‘react‘;
import ReactDOM from ‘react-dom‘;
import {Provider} from ‘react-redux‘

import App from ‘./containers/app‘;
import store from ‘./redux/store‘

ReactDOM.render(
    <Provider store={store}>
        <App/>
    </Provider>, document.getElementById(‘root‘)
);
index.js

 

以上是关于42_redux_counter应用_redux异步版本的主要内容,如果未能解决你的问题,请参考以下文章

终止日期后终止应用程序(自杀代码Redux)

在 Axios 中使用 Redux-Observable 的问题

Redux-DevTools安装

在 redux 状态树中添加新数据

有没有人在使用redux dev工具在TS中遇到这个错误? “'窗口'类型中不存在”属性'__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'。“

归档基于 Xcode 错误的 Unity 应用程序:无效的位码版本(生产者:'802.0.42.0_0' 读者:'800.0.42.1_0')