当前位置: 首页 > news >正文

服务性企业网站广州建设工程交易中心网站

服务性企业网站,广州建设工程交易中心网站,seo排名赚app最新版本,网站建设的基本流程包括什么Redux-Toolkit是为了简化使用Redux繁琐的步骤#xff0c;可以j降低使用useReducer与useContext管理状态的频率#xff0c;而且起到项目中状态管理规范和约束化的效果。 阅读本文需要的前置知识#xff1a;React、Redux、Typescript、Redux hooks。 Redux-Toolkit使用步骤 …Redux-Toolkit是为了简化使用Redux繁琐的步骤可以j降低使用useReducer与useContext管理状态的频率而且起到项目中状态管理规范和约束化的效果。 阅读本文需要的前置知识React、Redux、Typescript、Redux hooks。 Redux-Toolkit使用步骤 目前使用Redux-Toolkit管理消费redux状态的方式。举个例子假设我们现在的业务和银行转账有关有两个状态存在redux分别为银行账号和金额accountOfBank和amountOfBank使用createSlice来创建reducer和actions 首先安装redux-toolkit依赖包 npm i reduxjs/toolkit npm i react-redux 文件目录 slice.ts 创建切片声明存储状态对象以及action。createSlice创建一个切片slice主要参数 nameslice的标识在redux-devtool中会显示对应的名字initialState初始值对象reducers对象类型以及函数类型函数参数state和传递的action参数extraReducers用于处理异步比如网络请求等 creactSlice返回值是一个对象包含所有的actions。  import { createSlice, PayloadAction } from reduxjs/toolkit;type InitialState {accountOfBank: string,amountOfBank: string }const initialState: InitialState {accountOfBank: JanPan Bank,amountOfBank: 1000 }const slice createSlice({name: bank,initialState,reducers: {updateBankAccount: (state: InitialState, action: PayloadActionstring) {state.accountOfBank action.payload;},updateBankAmount: (state: InitialState, action: PayloadActionstring) {state.amountOfBank action.payload;}} })export const {updateBankAccount, updateBankAmount} slice.actions; export default slice.reducer; store.ts 存在在store中configureStore是Redux-Tookit的一个工厂函数用于创建Redux-Store。 store是通过传入一个reducer缩减器来创建的并通过getState的方法用于返回当前的状态值在Typescript强类型声明中有很大的帮助。 configureStore主要参数 reducer将slice的reducer传入middleware中间件devTools是否配置devTools工具默认为true import { configureStore } from reduxjs/toolkit; import slice from ./slice;export const store configureStore({reducer: {bank: slice} })export type BankState ReturnTypetypeof store.getState; index.tsx import React from react; import ReactDOM from react-dom/client; import App from ./App; import { Provider } from react-redux; import { store } from ./toolkit/store; const root ReactDOM.createRoot(document.getElementById(root) as HTMLElement ); root.render(Provider store{store}App //Provider );BankView.tsx 页面UI组件渲染useSelector获取当前管理的statestate的类型可通过store.ts的getState获取 并且将配置在store的reducer提取解构。 import { useDispatch, useSelector } from react-redux; import { BankState } from ../../toolkit/store; import { updateBankAccount, updateBankAmount } from ../../toolkit/slice;export const BankView () {const {accountOfBank, amountOfBank} useSelector((state: BankState) state.bank)const dispatch useDispatch();return (divh3accountOfBank - {accountOfBank}/h3h3amountBank - {amountOfBank}/h3button onClick{() dispatch(updateBankAccount(England))}change bank account/buttonspan /spanbutton onClick{() dispatch(updateBankAmount(2000))}change bank amount/button/div) } 详细目录截图如下 经过上面简单的封装就实现了全局状态管理使用简单高效而且可以实现业务与UI的解耦。 在网络请求等情况通常需要异常更新、提交数据等可通过createAynceThunk实现 import { createAsyncThunk, createSlice, PayloadAction } from reduxjs/toolkit;type InitialState {accountOfBank: string,amountOfBank: string,creatTime: string }const initialState: InitialState {accountOfBank: JanPan Bank,amountOfBank: 1000,creatTime: 2024-04-02 }export const fetchAccountBank createAsyncThunk(https://wwww.baidu.com,() {return new Promisestring(resolve {let timeId setTimeout(() {clearTimeout(timeId);resolve(2024-04-03);}, 1000)})})const slice createSlice({name: bank,initialState,reducers: {updateBankAccount: (state: InitialState, action: PayloadActionstring) {state.accountOfBank action.payload;},updateBankAmount: (state: InitialState, action: PayloadActionstring) {state.amountOfBank action.payload;}},extraReducers: (builder) {builder.addCase(fetchAccountBank.pending, (state: InitialState) {console.log(fetchAccountBank pending)state.creatTime loading...});builder.addCase(fetchAccountBank.fulfilled, (state, action) {console.log(action.payload)state.creatTime action.payload;console.log(fetchAccountBank fulfilled)});builder.addCase(fetchAccountBank.rejected, (state) {console.log(fetchAccountBank rejected)state.creatTime failed...})} })export const {updateBankAccount, updateBankAmount,} slice.actions; export default slice.reducer;因为Typescript语言有较强的类型校验在异步时dispatch报错如下 只需在使用useDispatch时声明泛型类型即可。 // 异步需声明useDispatch类型const asyncDispatch useDispatchAppDispatch(); 最佳实践全部代码如下 ReduxToolkitTypeScript最佳实践资源-CSDN文库
http://www.zqtcl.cn/news/14532/

相关文章:

  • 成都网站开发微信手机网页版
  • 金融网站织梦模板做家常便饭网站
  • 百度 新网站 重定向过多国内广告公司排行
  • 网站置顶jq注册公司代理记账头像图片
  • 建站系统排行榜辽阳市建设行业培训中心网站
  • 网站 图片防盗链win2003搭建wordpress
  • 怎么用php做网站方案263企业邮箱入口登录官网
  • 怎么自己做网站备案网站开发app开发
  • 网站首页页面设计多少钱常州网站建设托管
  • 农业网站怎么做摄影网站有哪些功能
  • 兼职 网站 小程序 建设网站没备案可以使用了吗
  • 音乐网站开发答辩ppt网页微信版扫描确认登录后无法显示此页
  • 什么是域名系统 网站建设教程wordpress post下载文件
  • 站长之家怎么找网址自助网站制作
  • 广西专业做网站的公司网站正能量视频不懂我意思吧
  • 郑州网站设计 郑州网站开发东莞南城房价
  • 全国最好的加盟网站怎么关闭seo查询
  • 网站推广是网站建设完成之后的长期工作成都专业做网站推广电话
  • 有那些猎头做单的网站深圳龙华建设工程交易中心网站
  • 官方网站建设 就问磐石网络专业未来的网站建设想法
  • 网站建设公司推荐 金石下拉如何在社交网站上做视频推广
  • 成都新津网站建设市场监督管理局电话号
  • 自己的网站到期域名如何续费前端开发能干到多少岁
  • 赣州高端网站开发医疗协助平台网站建设方案
  • 厦门网站推广¥做下拉去118cr建设简单网站的图纸
  • 外贸网站推广方式wordpress数据表优化
  • 贵阳网站方舟网络最好最新新闻热点事件2023年10月
  • 靖江网站建设公司基础网站建设的实施步骤
  • 东莞市专注网站建设怎么样搜狗推广
  • 商城网站有什么好处网站首页缩略图 seo