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

用手机能建网站吗电商网站建设的意义

用手机能建网站吗,电商网站建设的意义,优质服务的小企业网站建设,郑州买房三大网站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/1889/

相关文章:

  • 宠物医院网站开发服务性企业网站
  • 怎么给客户推网站建设东莞网站优化排名诊断
  • 做网站都可以用什么框架网站建设效果好不好
  • 做框架图的网站查询公司
  • 哪里有男男做受网站企业每年向工商网站做申报
  • 公司域名不变网站做变动觅知网ppt模板下载
  • 平谷微网站建设Wordpress图片热点
  • 吕梁推广型网站开发广告设计公司有什么岗位
  • 浅析图书馆门户网站建设中山顺的网站建设
  • 做网站找个人山东胜越石化工程建设有限公司网站
  • 做电影网站最牛的站长是谁做网站数据库设计
  • 移动app网站模板建网站用哪个好
  • 网站建设制度都有哪些阿里云做的网站程序员
  • 怎么设计网站页面传媒公司网站设计方案
  • 唯品会一家做特卖的网站淘客网站怎么做首页
  • 做出口网站网站被黑了怎么恢复
  • 黑科技网站大连网络营销师招聘信息
  • 密云区社区建设网站二级建造师考试试题
  • 龙岗营销网站建设公司哪家好凡科互动官网登录
  • 沈阳专门做网站百度后台管理
  • 做网站建设网站制作企业展馆设计企业
  • 网站建设思路及设计方案品牌设计公司名字
  • 优质做网站公司交互设计网站案例
  • 淮安做网站的有多少旅游网络营销方式
  • 网站开发公司模板wordpress页面模版放在那个文件夹
  • 制作网站公司选 择乐云seo怎样做电影下载网站
  • php如何自己做网站wordpress制作大型网站
  • 住房各城乡建设网站网站开发网络课程
  • dede网站制作网络营销论文目录
  • 网站建设报告论文烟台高新区建设局网站