利用 typescript 写 react-redux 和 redux-thunk

2023-12-18 15:32

本文主要是介绍利用 typescript 写 react-redux 和 redux-thunk,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

react-redux 的常规使用步骤

  • Provider 作为顶层全局状态的提供者,需要传递一个参数,全局状态 store
import { Provider } from 'react-redux';
<Provider store={ store }></Provider>
  • store 由 createStore 函数创建生成,需要传递 reducer 纯函数作为参数
import { createStore, combineReducers } from 'redux';
const rootReducer = combineReducers({reducer
});
const store = createStore(rootReducer);
  • reducer 又接收两个参数,state 和 action,根据不同的 action 返回一个新的 state
const reducer = (state, action) => {switch (action) {default:return state;}
};
  • 接下来就可以在组件中使用 redux 了
  • mapStateToProps 会接收全局 state 作为参数,返回一个对象,对象的属性作为提供给组件的 props
{ props.reducer };const mapStateToProps = (state) => ({reducer: state.reducer
})
  • mapDispatchToProps 接收 dispatch 作为参数,返回的对象的属性是方法,方法中会调用这个 dispatch 进行更新,可以往 dispatch 中传递对象(只能触发一次dispatch)或者函数(可以触发任意次数,异步操作当然也可以)
props.action();const mapDispatchToProps = (dispatch) => ({action: () => dispatch(action)
})
  • actionCreator 用来生成传递给 dispatch 的参数,也就是通常会在 actionCreator 中返回 action 对象
// 返回对象
const actionCreator = function(data) {return {type: ACTION_TYPE,data}
}// 使用
props.action(someData);const mapDispatchToProps = (dispatch) => ({action: (data) => dispatch(actionCreator(data))
})

TypeScript 写法

上面的代码在 js 环境下执行没有问题,直接将文件名后缀改为 .ts,不出意外是会提示错误的……

例如:

// action.ts
// error: 参数“data”隐式具有“any”类型
export const CHANGE_NAME = 'CHANGE_NAME';
export const changeName = (data) => ({type: CHANGE_NAME,data
});// reducer.ts
// error: 参数“action”隐式具有“any”类型
import { CHANGE_NAME } from './action';
const initialState = {name: 'lixiao'
};
export default (state = initialState, action) => {switch (action.type) {case CHANGE_NAME:retutn Object.assign({}, state, {name: action.data});default:return state;}
};// rootReducer.ts
import { combineReducers } from 'redux';
import home from './pages/Home/reducer';
export default combineReducers({home
});// Home.tsx
// error: 参数“state”隐式具有“any”类型
//        参数“dispatch”隐式具有“any”类型
//        参数“data”隐式具有“any”类型
const mapStatetoProps = (state) => ({name: state.home.name
});
const mapDispatchToProps = (dispatch) => ({changeName: (data) => dispatch(changeName(data)),
})

ts 做了静态编译,一部分目的就是为了避免随意取属性这样的操作。例如 mapStateToProps 中 state.home 这样的操作,需要告诉 ts,参数中的 state 有哪些属性可以取,同样的道理,拆分之后的每一个 reducer 有哪些属性可以取,都需要明确的告诉 ts,(使用 any 类型就不太友好了)。接下来对这些提示错误的变量进行类型声明。

actionCreator

参数中的 data 作为该类型 action 的负载,变量类型设置为 any 是可以接受的

// action.ts
export const CHANGE_NAME = 'CHANGE_NAME';
export const changeName = (data: any) => ({type: CHANGE_NAME,data
});

reducer

reducer 接收 state 和 action 两个参数,action 通常会有一个动作类型属性 type 和 动作负载 data,可以在整个应用中统一为这个形式;而 这里的state 会在其他连接上 redux 的组件中进行访问,因此有必要定义好一个接口,约定能访问到这个 state 的哪些属性。

// global.d.ts
declare interface IAction {type: string;data: any;
}// reducer.ts
import { CHANGE_NAME } from './action';// 这个接口约定子 reducer 能访问到的属性
export interface IHomeState {name: string;
}
const initialState: IHomeState = {name: 'lixiao'
};
export default (state = initialState, action: IAction): IHomeState => {switch (action.type) {case CHANGE_NAME:return Object.assign({}, state, {name: action.data});default:return state;}
};

rootReducer

作为顶层 state,在子组件中通常会通过它再去访问子 reducer 中的 state,前面已经约定好了子 reducer 中的 state 能够取到哪些属性,现在还需要约定好顶层 state 能取到哪些子 state

// rootReducer.ts
import { combineReducers } from 'redux';
import home, { IHomeState } from './pages/Home/reducer';// 每增加一个子 reducer,都在这个接口中同步更新
export interface IStore {home: IHomeState
}
export default combineReducers({home
});

组件中使用 redux

// Home.tsx
import { Dispatch } from 'redux';
import { IStore } from '../../rootReducer';// 组件的 props 类型
interface IProps {name: string;// 这里的 changeName 和 actionCreator 中的不是同一个,所以返回值类型不是对象changeName(data: any): void;
}
const mapStatetoProps = (state: IStore) => ({name: state.home.name
});
// Dispatch 接收的参数为对象
const mapDispatchToProps = (dispatch: Dispatch) => ({changeName: (data: any) => dispatch(changeName(data)),
})

redux-thunk

在前面的例子中,将 action 传递给 dispatch,然后根据 reducer 函数返回新的 state,这个操作是同步的,类似于 vuex 中的 mutation。redux-thunk 是处理异步 action 的一种解决方式。

异步 action

一个很常见的场景,发送 ajax 请求成功之后触发 dispatch。最简单的实现方式是将这个操作封装成一个函数:

const asyncAction = () => {dispatch({ type, data });ajax().then(res => dispatch(res));
}

若这个操作需要在多个地方调用呢?复制粘贴这个函数也是可行的,但更好的做法是使用 redux-thunk 这样的中间件,在 actionCreator 中生成一个包含异步操作的 action

redux-thunk 用法

  • 在创建 store 的时传入 redux-thunk 中间件
// index.tsx
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';const store = createStore(rootReducer, applyMiddleware(thunk));
  • actionCreator 返回的是一个函数,在这个函数中可以做任意的事情,触发任意次数的 dispatch,当然也包括异步操作。
// action.ts
import { Dispatch } from 'redux';export const CHANGE_NAME = 'CHANGE_NAME';
export const changeName = (data: any) => ({type: CHANGE_NAME,data
});
export const changeNameAsync = (data?: any) => (dispatch: Dispatch) => {dispatch(changeName('loading'));fetch('/api').then(res => res.json()).then(res => dispatch(changeName(res.data)));
}
  • 在组件中使用这个返回函数的 action
// Home.tsx
import { changeName, changeNameAsync } from './action';// dispatch 可以传入对象、函数,这里不能直接简单的使用 Dispatch 类型
const mapDispatchToProps = (dispatch: any) => ({changeName: (data: any) => dispatch(changeName(data)),changeNameAsync: () => dispatch(changeNameAsync())
});
// 也可以使用 bindActionCreators
const mapDispatchToProps = (dispatch: Dispatch) => (bindActionCreators({changeName,changeNameAsync
}, dispatch))

redux-thunk 实现过程

使用一次之后就发现,同步 action 与异步 action 的区别在于,同步操作 dispatch 一个对象,而异步操作是 dispatch 一个函数,在这个函数中可以包含异步、dispatch 同步 action 等任意操作。

createStore

基础版本

在引入 redux-thunk 之前,生成 store 的方式为

const store = createrStore(reducer, initialState)

第二个参数 initialState 为初始状态,可选

// 两个参数
function createStore(reducer, preloadedState) {let currentReducer = reducer;let currentState = preloadedState;// 比较重要的函数 dispatchfunction dispatch(action) {// 这就是为什么也可以在 reducer 中初始化 state// 也可以发现这里的参数 preloadedState 的优先级高于 reducer 中的 initialStatecurrentState = currentReducer(currentState, action);}// 页面刚打开的时候,调用 createStore,再执行一次 dispatch,更新 currentState// 所以 redux 开发工具中会有一个 @@INIT 类型的 actiondispatch({ type: ActionTypes.INIT });
}

高级版本

引入 redux-thunk 之后,生成 store 的方式也有所变化

const store = createrStore(reducer, initialState, enhancer)

第三个参数是一个函数,接下来看看传入第三个参数时, createStore 内部是如何处理的

function createStore(reducer, preloadedState, enhancer) {// 由于第二个参数是可选的,所以需要一些代码进行参数数量和参数类型的检测// ts 就方便了……// 检测通过之后就提前 return 了,return enhancer(createStore)(reducer, preloadedState)
}

进行一下代码转换

const store = createStore(rootReducer, applyMiddleware(thunk));
// enhancer 就是 applyMiddleware(thunk)
// preloadedState 为 undefined
// 代码转换
const store = applyMiddleware(thunk)(createStore)(rootReducer);

applyMiddleware

import compose from './compose';export default function applyMiddleware(...middlewares) {return createStore => (...args) => {const store = createStore(...args)let dispatch = () => {throw new Error('Dispatching while constructing your middleware is not allowed. ' +'Other middleware would not be applied to this dispatch.')}const middlewareAPI = {getState: store.getState,dispatch: (...args) => dispatch(...args)}const chain = middlewares.map(middleware => middleware(middlewareAPI))dispatch = compose(...chain)(store.dispatch)return {...store,dispatch}}
}

接着进行代码装换

const store = applyMiddleware(thunk)(createStore)(rootReducer);
// 相当于在执行下面这串代码
const store = createStore(rootReducer) // 这个 store 是基础版本中的 store
let dispatch = () => {throw new Error('Dispatching while constructing your middleware is not allowed. ' +'Other middleware would not be applied to this dispatch.')
}const middlewareAPI = {getState: store.getState,// dispatch: (rootReducer) => dispatch(rootReducer)dispatch: dispatch
}
const chain = [thunk].map(middleware => middleware(middlewareAPI))
dispatch = compose(...chain)(store.dispatch)
// 这是最终返回的 store 
return {...store,dispatch
}

再看看 thunk(middlewareAPI)compose 做了什么

thunk

// redux-thunk/src/index.js 简化之后
const thunk = ({ dispatch, getState }) => next => action => {if (typeof action === 'function') {return action(dispatch, getState);}return next(action);
};

compose

function compose(...funcs) {if (funcs.length === 0) {return arg => arg}if (funcs.length === 1) {return funcs[0]}return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

传入的多个中间件,会依次作为参数;只有一个中间件则直接返回这个函数

dispatch 怎么被拓展的

const store = createStore(rootReducer)
// const chain = [thunk].map(middleware => middleware(middlewareAPI))
// const chain = [thunk(middlewareAPI)];
const chain = [(next => action => {if (typeof action === 'function') {return action(dispatch, store.getState);}return next(action);
})]
// dispatch = compose(...chain)(store.dispatch)
dispatch = action => {if (typeof action === 'function') {return action(dispatch, store.getState);}return store.dispatch(action);
}

当传给 dispatch 的参数不是函数时,就是基础版本,直接调用 reducer 函数进行更新 state;传入的是函数时,执行函数体的内容并把 dispatch 传进去,在这个函数内部就又可以使用 dispatch 做想做的事情了。

总结

大部分函数都是 redux 提供的,闭包用的比较多,(params1) => (params2) => { someFunction(params1, params2) } 这样的用法很多,看起来容易头晕,但也就是每执行一次函数传入一次参数。

这篇关于利用 typescript 写 react-redux 和 redux-thunk的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/508922

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

使用Python和Tkinter实现html标签去除工具

《使用Python和Tkinter实现html标签去除工具》本文介绍用Python和Tkinter开发的HTML标签去除工具,支持去除HTML标签、转义实体并输出纯文本,提供图形界面操作及复制功能,需... 目录html 标签去除工具功能介绍创作过程1. 技术选型2. 核心实现逻辑3. 用户体验增强如何运行

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽