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

西双版纳 网站建设设计公司做网站有用吗

西双版纳 网站建设,设计公司做网站有用吗,厦网站建设培训学校,海淀视频网站建设reactredux异步操作数据 redux中操作异步方法#xff0c;主要是#xff1a; 1、借助createAsyncThunk()封装异步方法#xff1b;2、通过extraReducers处理异步方法触发后的具体逻辑#xff0c;操作派生的state 1、异步操作的slice import { createSlice, createAsyncThunk…reactredux异步操作数据 redux中操作异步方法主要是 1、借助createAsyncThunk()封装异步方法2、通过extraReducers处理异步方法触发后的具体逻辑操作派生的state 1、异步操作的slice import { createSlice, createAsyncThunk } from reduxjs/toolkit// 使用该类型定义初始 state const initialState {systemName: 三分之一 } // promise封装的定时器 function delay(ms: number, data: string) {return new Promise((resolve) setTimeout(() resolve(data), ms)) } // AsyncThunkvoid, void, AsyncThunkConfig // createAsyncThunkstring, string, object : // 第一个string: system/updateSystemName // 第二个string: updateSystemName调用时传的参数 // object AsyncThunkConfig 配置对象 // 详细可见 https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk export const updateSystemName createAsyncThunkstring, string(system/updateSystemName,async (data, config): Promisestring {console.log(data, config)const res await delay(2000, data)return res as string} )const systemSlice createSlice({name: system,initialState,reducers: {},extraReducers: (builder) {// 触发updateSystemName执行builder.addCase的回调builder.addCase(updateSystemName.fulfilled, (state, action) {// action: {// type: system/updateSystemName/fulfilled// },// payload: dispatch(updateSystemName(单点的)) updateSystemName// meat: {arg: 单点的, ...}state.systemName action.payload})} }) export default systemSlice.reducerAsyncThunkConfig 如图所示 2、组件中调用异步的方法 import { updateSystemName } from /store/reducers/systemSlice import { RootState } from /store import { useAppDispatch } from /hooks/useAppDispatch import { useAppSelector } from /hooks/useAppSelectorconst Home () {const { systemName } useAppSelector((state: RootState) state.systemReducer)const dispatch useAppDispatch()const test () {// useDispatch() 返回值函数默认期望的参数类型是 AnyAction// 异步处理是updateSystemName AsyncThunkAction// 所以这块使用官网推荐的自定义封装的hooks useAppDispatchdispatch(updateSystemName(单点的))}return (divhome page/divp{systemName}/pbutton onClick{test}测试/button/) }export default Home4、给dispatch()参数添加Action类型 解决 类型“AsyncThunkActionstring, string, AsyncThunkConfig”的参数不能赋给类型“AnyAction”的参数。 useAppDispatch import type { AppDispatch } from /store import { useDispatch } from react-redux// 给useDispatch 添加泛型 默认接收的参数是AnyAction // import { useDispatch } from react-redux export const useAppDispatch () useDispatchAppDispatch()useAppSelector import type { RootState } from /store import { TypedUseSelectorHook, useSelector } from react-redux// import { useSelector } from react-redux export const useAppSelector: TypedUseSelectorHookRootState useSelectorstore中 export type RootState ReturnTypetypeof store.getState export type AppDispatch typeof store.dispatch import { configureStore } from reduxjs/toolkit import { combineReducers } from redux // 数据持久化 import { persistStore, persistReducer } from redux-persist import storageLocation from redux-persist/lib/storage // defaults to localStorage for web // redux-persist/lib/storage/session // reducers import userReducer from ./reducers/userSlice import systemReducer from ./reducers/systemSlice const persistConfig {key: root,storage: storageLocation } // 持久化reducers const persistedReducer persistReducer(persistConfig,combineReducers({//数据切片userReducer,systemReducer}) )const store configureStore({// userReducer 模块名reducer: persistedReducer,middleware: (getDefaultMiddleware) getDefaultMiddleware({serializableCheck: false}) })// 可以订阅 store // store.subscribe(() console.log(store.getState(), userSlice))// 持久化的store const persistor persistStore(store)export { store, persistor }// Infer the RootState and AppDispatch types from the store itself export type RootState ReturnTypetypeof store.getState // Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} export type AppDispatch typeof store.dispatch
http://www.yutouwan.com/news/88598/

相关文章:

  • 服装高级定制广州短视频seo哪家好
  • 给网站做cdn2013电子商务网站建设考试试卷
  • 如何创建自己的公司网站哪里免费做网站
  • iis本地网站wordpress 缓存 自适应
  • 亿联时代网站建设建一个英文网站需要多少钱
  • 电子商务网站后台核心管理多语种网站制作
  • 制作类似网站软件开发的某网站建设公司网站源码
  • 中国男女直接做的视频网站网站反链数
  • 临沂河东建设局网站商城网站互动性
  • 做旅游广告在哪个网站做效果好上海英文网站建设公司
  • 网站推广专业术语贵阳网站建设多少钱
  • 建设企业学习网站列出网站目录
  • 廊坊专业网站建设丰涵网站建设
  • 网站界面设计的发展域名注册查询阿里云
  • 石家庄平山网站推广优化WordPress mvc插件
  • 华为云建站视频教程php网站开发过程考试
  • anker 网站建设菜馆网站制作
  • wordpress网站跳转随州网站建设哪家专业
  • 利于seo的网站设计下载搭建网站软件
  • 淘宝做网站为什么那么便宜设计微信公众号的网站吗
  • 做网站用哪几个端口 比较好徐州模板网站
  • 莱州网站建设263企业邮箱入口登录找回密码
  • 石岩网站设计哪里有网站建设加工
  • 小昆山网站建设鲜花网站建设的主要工作流程
  • 自己怎么做网站免费的wordpress播放百度云
  • 万维网域名注册网站优化推广网站排名
  • 网站建设提高信息wordpress电子邮件注册
  • 如何建立一个学校网站制作京东一样的网站
  • 分销系统商城定制开发西安seo培训学校
  • 成都网站排名优化开发网站设计公司建设