首页 > 其他分享 >redux7.x基本实现记录

redux7.x基本实现记录

时间:2023-01-18 14:44:19浏览次数:33  
标签:基本 COUNT .. 记录 redux7 count state import redux

1、下载相关依赖

npm i [email protected] [email protected] [email protected]  [email protected]  [email protected] [email protected] @types/[email protected]
@types/[email protected] @types/[email protected]

store - state - reducer - actions

2、configureStore

import { createStore, applyMiddleware } from "redux"
import thunkMiddleware from 'redux-thunk'
import { createLogger } from 'redux-logger'
import reducers from "./reducers"

const loggerMiddleware: any = createLogger()

let middlewares = [thunkMiddleware]

if (process.env.NODE_ENV == 'development') {
    middlewares.push(loggerMiddleware)
}
let composeEnhancers = applyMiddleware(...middlewares)

export default function configureStore(initialState?: any) {
    return composeEnhancers(createStore)(reducers, initialState)
}

3、reducers

import { combineReducers } from 'redux'
import test from "./reducers/test";

const reducers = combineReducers({
    test
})

export default reducers;

reducers/test.ts

import * as constants from "../actionTypes"
import { ITest } from "../states/test";

const initialState: ITest = {
    count: 0
}

interface ITestAction {
    type: string
    data: number
}

export default function addCount(
    state: ITest = initialState,
    action: ITestAction
) {
    switch (action.type) {
        case constants.ADD_COUNT:
            return {
                ...state,
                count: state.count + 1
            }
        case constants.SUB_COUNT:
            return {
                ...state,
                count: state.count - 1
            }
        case constants.SET_COUNT:
            return {
                ...state,
                count: action.data
            }
        default:
            return state
    }
}

action types

// test action type
export const ADD_COUNT = "ADD_COUNT"
export const SUB_COUNT = "SUB_COUNT"
export const SET_COUNT = "SET_COUNT"

4、页面引入及使用

import * as React from "react";
import { connect } from "react-redux";
import * as dataAPI from '../../api/dataAPI';
import { IRootState } from "../../redux/states";
import * as test from "../../redux/reducers/test"
import { ADD_COUNT } from "../../redux/actionTypes";

const Login = ({count, addCount}) => {

    const testApi = () => {
        dataAPI.getBlockNumber().then(res => {
            console.log(res)
        })
    }

    const add = () => {
        addCount(1)
        setTimeout(() => {
            console.log('>>>',count)
        }, 3000);
    }

    testApi()

    return (
        <div className="signFlowLoginPage">
            <div>测试redux数据:{count}</div>
            <button onClick={add}>+</button>
            <button>-</button>
            <button>15</button>
        </div>
    );
}

export default connect(
    (state: IRootState) => {
        console.log(">>>>>", state)
        return {
            count: state.test.count
        }
    },
    dispatch => ({
        addCount: (num) => dispatch({type: ADD_COUNT, num} )
    })
)(Login);

 

标签:基本,COUNT,..,记录,redux7,count,state,import,redux
From: https://www.cnblogs.com/Nyan-Workflow-FC/p/17059761.html

相关文章

  • 使用dayjs关于日期格式化的一些记录
    本篇文章主要是看了下面这篇博文和知乎上的讨论出现的http://www.cielni.com/2020/01/10/java-date-format/#morehttps://zhuanlan.zhihu.com/p/100648038通常前端会使......
  • 读书笔记:价值投资.零七.我对投资的基本理解
    我对投资的基本理解其实价值投资是所谓投资的唯一一条路.不存在什么捷径,不存在什么窍门.如果你想走轻松的路,我保证:它比艰难的路更长更痛苦.问题是拿利......
  • Kubernetes 基本概念
    ContainerContainer(容器)是一种便携式、轻量级的操作系统级虚拟化技术。它使用namespace隔离不同的软件运行环境,并通过镜像自包含软件的运行环境,从而使得容器可以很方便的......
  • 基本的SELECT语句与显示表结构
    SELECT...SELECT1+1,2+2;#直接这样写相当于下面这句SELECT1+1,2+2FROMDUAL;#这里DUAL:伪表SELECT...FROM语法:SELECT标识选择哪些字段(列)FROM标识从哪个表中选......
  • 前端Linux部署命令与流程记录
    以前写过一篇在Linux上从零开始部署前后端分离的Vue+Springboot项目,但那时候是部署自己的个人项目,磕磕绊绊地把问题解决了,后来在公司有了几次应用到实际生产环境的经验,发......
  • 自动化记录
    1.在/root/nginx/logs目录下,创建20个日志文件,从2019-03-01到2019-03-20。并且,将系统日期修改为2019-03-20。fortimein{01..20};dodate-s"201903$time";touch/r......
  • vue基本知识回顾 | this.$http.get 和 this.$http.post传参 / created与mounted区别 /
    vue基本知识回顾|this.$http.get和this.$http.post传参/created与mounted区别/富文本解析https://blog.csdn.net/feng2qing/article/details/126241834vue使......
  • Xshell连接虚拟机Linux踩坑记录
    ​​欢迎光临我的个人主页​​我使用的是桥接模式,没有做其他修改网卡配置的设置,在windows下可以ping通虚拟机上的Linux,但是Xshell依旧连不上Linux在终端输入:ps-e|g......
  • Charles 使用记录
    Charles使用记录前言本文使用版本Charles4.6.3功能截取Http和Https网络封包。支持重发网络请求,方便后端调试。支持修改网络请求参数。支持网络请求的截获并......
  • mysql记录锁(record lock),间隙锁(gap lock),Nextkey锁(Nextkey lock)
    1.什么是幻读?幻读是在可重复读的事务隔离级别下会出现的一种问题,简单来说,可重复读保证了当前事务不会读取到其他事务已提交的UPDATE操作。但同时,也会导致当前事务无法感......