react学习——25redux实现求和案例(完整版)

1、目录结构
在这里插入图片描述
2、count/index.js

import React, {Component} from "react";
//引入store,用于获取数据
import store from '../../redux/store'
//引入actionCreator 专门创建action对象
import {createDecrementAction,createIncrementAction} from '../../redux/count_action'

export default class Count extends Component {
    state = {
        count:store.getState()
    }
    componentDidMount() {
        //监测redux中的数据的变化,只要改变,就会调用render
        store.subscribe(()=>{
            this.setState({
                count:store.getState()
            })
        })
    }
    //加法
    increment=()=>{
        const {value} = this.selectNum
        //通知redux 改变
        // store.dispatch({type:'increment',data:parseInt(value)})
        store.dispatch(createIncrementAction(parseInt(value)))
     }
     //减法
    decrement=()=>{
        const {value} = this.selectNum
        store.dispatch(createDecrementAction(parseInt(value)))
     }
     //奇数加
    incrementIfOdd=()=>{
        const {value} = this.selectNum
        const {count} = this.state
        if(count%2!==0)
        {
            store.dispatch(createIncrementAction(parseInt(value)))
        }
     }
     //异步加
    incrementAsync=()=>{
        const {value} = this.selectNum
        setTimeout(()=>{
            store.dispatch(createIncrementAction(parseInt(value)))
        },500)
     }
    render()
    {
        return(
            <div>
               <h1>当前求和为:{store.getState()}</h1>
                <select ref={(c)=>this.selectNum=c}>
                    <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}>当前求和为奇数在加</button>&nbsp;
                <button onClick={this.incrementAsync}>异步加</button>&nbsp;
            </div>
        );
    }

}

2、redux/constant.js

/*
    该模块用于定义状态管理的常量名称,目的只有一个:便于管理的同时,防止程序员单词拼写错误
*/
export const INCREMENT = 'increment'
export const DECREMENT = 'decrement'

redux/count_action.js

/*
    该文件专门为Count组件生成action对象
 */
import {INCREMENT,DECREMENT} from './constant'
// function createIncrementAction(data){
//     return {type:'increment',data}
// }
export const createIncrementAction = data=>({type:INCREMENT,data})
// function createDecrementAction(data){
//     return {type:'decrement',data}
// }
export const createDecrementAction = data=>({type:DECREMENT,data})

// export default {
//     createIncrementAction,
//     createDecrementAction
// }

redux/count_reducer.js

/*
    1.该文件是用于创建一个为count服务的reducer,reducer的本质就是一个函数
    2.reducer函数会接到两个参数,分别为:之前的状态(preState),动作对象(action)
*/
import {INCREMENT,DECREMENT} from "./constant";

const initState = 0
export default function countReducer(preState=initState,action) {
    console.log('countReducer',preState,action)
    const {type,data} = action
    // 根据type决定如何加工数据
    switch (type){
        case INCREMENT:
            console.log('@')
            return preState + data
        case DECREMENT:
            return preState - data
        default:
            return preState
    }
}

/redux/store.js

/*
    该文件专门用于创建一个为Count组件服务的store对象
 */
//引入createStore,专门创建redux中最为核心的store对象
import {createStore} from 'redux'
//引入cunter.redux.js文件
import countReducer from './count_reducer'
//暴露store
export default createStore(countReducer)

3、src/App.js

import React, {Component} from "react";
import Count from "./components/Count";
export default class App extends Component {
    render()
    {
        return(
            <div>
                <Count></Count>
            </div>
        );
    }
}

src/index.js

import React from "react";
import ReactDOM from 'react-dom'
import App from './App'
ReactDOM.render(<App/>,document.getElementById('root'))

相关推荐

  1. react学习-redux快速体验

    2024-07-11 01:18:05       14 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 01:18:05       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 01:18:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 01:18:05       4 阅读
  4. Python语言-面向对象

    2024-07-11 01:18:05       4 阅读

热门阅读

  1. GESP C++ 三级真题(2023年9月)T2 进制判断

    2024-07-11 01:18:05       7 阅读
  2. qt中connect函数的使用方法

    2024-07-11 01:18:05       12 阅读
  3. C编程题收藏

    2024-07-11 01:18:05       9 阅读
  4. 深入Django(六)

    2024-07-11 01:18:05       9 阅读
  5. django models对应的mysql类型

    2024-07-11 01:18:05       9 阅读
  6. Golang 输入与输出

    2024-07-11 01:18:05       7 阅读