React+Typescript项目环境中搭建并使用redux环境

news/2024/7/15 18:41:35 标签: react.js, typescript, 前端

前几篇文章 我们的项目已经开始功能渐渐完善了
那么 我们来说最后一个点 redux
这个并不需要我们多努力 其实官方文档给到已经算是很全面了

我们可以直接访问地址 TypeScript 中文手册中文手册和官方是一样的 而且对我们非常友好
我们会在左侧导航栏中找到一个 React 点进去
在这里插入图片描述
进入之后 一直往下翻 我们就可以看到 Redux 部分
在这里插入图片描述
我们直接 用他这个命令去项目终端安装就好了 下面也讲述了为什么不需要单独再去导入Redux了
在这里插入图片描述
我们这里在项目终端去引入
在这里插入图片描述
这里需要注意一下版本哦 反正 我发现 react开发 除了文档不是特别健全之外 每个版本改动都还是不小的 经常出现 一个版本提升 就写法完全不一样了的事
在这里插入图片描述
我们在项目src目录下创建一个文件夹 叫 types
下面创建一个 index.tsx
参考代码如下

typescript">export interface IStoreState {
    languageName: string;
    enthusiasmLevel: number;
}

就是简单导出了一个接口 接口限制要有两个字段 languageName 字符串类型 enthusiasmlevel 数字类型

我们在项目src目录创建一个文件夹 叫 constants
下面创建一个 index.tsx文件
参考代码如下

typescript">export const INCREMENT_ENTHUSIASM = 'INCREMENT_ENTHUSIASM';
export type INCREMENT_ENTHUSIASM = typeof INCREMENT_ENTHUSIASM;
export const DECREMENT_ENTHUSIASM = 'DECREMENT_ENTHUSIASM';
export type DECREMENT_ENTHUSIASM = typeof DECREMENT_ENTHUSIASM;

这里声明了两个常量 至于type的写法 大家可以去看一下 ts命名空间的内容
我们简单说 通过type 声明一个自己的字符串字面量类型

然后 我们再在项目src目录创建一个目录 叫 actions
目录下依旧创建一个 叫 index.tsx 的文件
参考代码如下

typescript">import * as constants from '../constants'

export interface IIncrementEnthusiasm {
    type: constants.INCREMENT_ENTHUSIASM;
}

export interface IDecrementEnthusiasm {
    type: constants.DECREMENT_ENTHUSIASM;
}

export type EnthusiasmAction = IIncrementEnthusiasm | IDecrementEnthusiasm;

export function incrementEnthusiasm(): IIncrementEnthusiasm {
    return {
        type: constants.INCREMENT_ENTHUSIASM
    }
}

export function decrementEnthusiasm(): IDecrementEnthusiasm {
    return {
        type: constants.DECREMENT_ENTHUSIASM
    }
}

然后 我们再在src目录下创建 reducer 文件夹
下面还是一个index.tsx
参考代码如下

typescript">import { EnthusiasmAction } from '../actions';
import { IStoreState } from '../types/index';
import { INCREMENT_ENTHUSIASM, DECREMENT_ENTHUSIASM } from '../constants/index';

export function enthusiasm(state: IStoreState, action: EnthusiasmAction): IStoreState {
  switch (action.type) {
    case INCREMENT_ENTHUSIASM:
      return { ...state, enthusiasmLevel: state.enthusiasmLevel + 1 };
    case DECREMENT_ENTHUSIASM:
      return { ...state, enthusiasmLevel: Math.max(1, state.enthusiasmLevel - 1) };
    default:
      return state;
  }
}

这就相当于是我们之前 全局处理 接受/返回 的一个地方
我们分别写了两个事件 对应 将 enthusiasmLevel 加一 和 减一
事件名就对应我们上面定义的两个变量值 DECREMENT_ENTHUSIASM与INCREMENT_ENTHUSIASM

然后 我们再在src目录下创建一个文件夹 store
然后下面再创建一个 文件 叫 index.tsx

typescript">import { createStore, Reducer } from 'redux';
import { enthusiasm } from '../reducer/index';
import { EnthusiasmAction } from '../actions/index';
import { IStoreState } from '../types/index';

const initialState: IStoreState = {
  enthusiasmLevel: 1,
  languageName: 'TypeScript'
};

const store = createStore<IStoreState, EnthusiasmAction, {}, {}>(enthusiasm as Reducer<IStoreState, EnthusiasmAction>, initialState);

export default store;

这个 initialState 就是我们的数据来源了 里面有两个值 分别是 enthusiasmLevel和languageName
接下来 我们重点就是关联了

我们打开 src下的 index.tsx
引入

typescript">import { Provider } from "react-redux";
import store from "./store";

然后 在标签上 用上 Provider store 属性 就用我们写的 store
在这里插入图片描述
然后 这里 我也有点懒 直接就给代码写到 App.tsx里吧
App.tsx编写代码如下

typescript">import * as React from "react";
import * as actions from './actions/index';
import { IStoreState } from './types/index';
import { connect } from 'react-redux';
export interface IProps {
  name: string;
  enthusiasmLevel?: number;
  onIncrement?: () => void;
  onDecrement?: () => void;
}
class App extends React.Component<IProps,any> {
  public render() {
    const { name,enthusiasmLevel,onIncrement,onDecrement } = this.props;
    return (
      <div className="App">
        <p>{ name }</p>
        <p>{ enthusiasmLevel }</p>
        <button onClick={ onIncrement }>+</button>
        <button onClick={ onDecrement }>-</button>
       
      </div>
    );
  }
}
export function mapStateToProps({ enthusiasmLevel, languageName }: IStoreState) {
  return {
    enthusiasmLevel,
    name: languageName,
  }
}

export function mapDispatchToProps(dispatch: any) {
  return {
    onDecrement: () => dispatch(actions.decrementEnthusiasm()),
    onIncrement: () => dispatch(actions.incrementEnthusiasm())
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

这样 我们运行起项目 会看到 name 和 enthusiasmLevel 的展示是正常的
在这里插入图片描述
然后 我们操作 decrementEnthusiasm 和 incrementEnthusiasm 加减事件 我们会发现

虽然能够正常执行 但实际上 , 也是非常的好用啊
在这里插入图片描述


http://www.niftyadmin.cn/n/5039071.html

相关文章

git提示:remote origin already exists

目录 问题场景 问题原因 问题解决 问题场景 在GitLab中新建仓库后&#xff0c;然后将本地项目提交提示&#xff1a;remote origin already exists. 问题原因 error: remote origin already exists. 错误&#xff1a;远程源点已存在&#xff08;翻译&#xff09; 出现该错误的…

python opencv图片二值化后取出图片中心区域的轮廓

python opencv图片二值化后取出图片中心区域的轮廓 1.导入必要的库&#xff1a; import cv2 import numpy as np2.读取图片并将其转为灰度图像&#xff1a; image cv2.imread(image.jpg) gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)3.对灰度图像进行二值化处理&#xf…

RxJS:前端开发的未来

引言 随着前端开发的不断发展&#xff0c;我们面临着越来越复杂的应用程序和更高的用户期望。为了应对这些挑战&#xff0c;开发人员需要使用更高效、更灵活的工具和技术。RxJS&#xff08;Reactive Extensions for JavaScript&#xff09;是一个强大的库&#xff0c;它提供了…

C++虚函数剖析-继承,多继承和虚继承情况

tags: C categories: C 写在前面 前面分析了 C类内的虚指针和虚表, 通过二级指针解引用的方式找到虚表, 由此访问虚函数, 相较于传统的死记硬背, 我一直觉得学习编程时候能看到具体的/确切的输入输出结果, 对于掌握某个知识点要更加有效, 如果你只是知道了虚函数的原理, 却又…

高通8295中国首发!智舱再度上演「军备竞赛」,这次有何不同

对于智能座舱来说&#xff0c;上一轮市场红利已经接近尾声。 高工智能汽车研究院监测数据显示&#xff0c;2023年1-6月中国市场&#xff08;不含进出口&#xff09;乘用车前装标配中控娱乐系统交付856.13万辆&#xff0c;前装标配渗透率已经超过90%&#xff0c;达到92.13%。 …

蓝桥杯每日一题2023.9.12

蓝桥杯2022年第十三届决赛真题-卡牌 - C语言网 (dotcpp.com) 题目描述 这天&#xff0c;小明在整理他的卡牌。 他一共有 n 种卡牌&#xff0c;第 i 种卡牌上印有正整数数 i(i ∈ [1, n])&#xff0c;且第 i 种卡牌 现有 ai 张。 而如果有 n 张卡牌&#xff0c;其中每种卡牌…

axios全局路由拦截的设置方法

一个项目中如果http请求发生了错误/异常&#xff0c;比如返回码4xx&#xff08;表示没有授权&#xff0c;登录过期等&#xff09;&#xff0c;我们希望能够在axios在第一时间就能拦截获取到&#xff0c;然后直接提示报错的错误信息&#xff0c;而不是在发起请求的地方&#xff…

【map、set的封装】

目录 前言一、map、set的框架搭建二、map、set的迭代器的封装2.1、map、set的迭代器的初步封装2.2、map、set的const迭代器的封装2.2.1、set的const迭代器的封装2.2.2、map的const迭代器的封装 三、operator[ ]四、代码实现 前言 本文的代码是基于前一篇文章的红黑树的代码来封…