我使用的是Reaction,我得到了一个Redux-Toolkit(RTK)状态片,它与本地存储(我使用的是redux-persist库)一起持久化.简化后是这样的:

const initLocations = []

const locationsPersistentSlice = createSlice({
  {
  name: "locations",
  initLocations,
  reducers: {
    addLocation(prevState, action) {
      // adding new location to store
    },
    clearLocations() {
      return []
    }
  }
})

它可以同步、完美地工作.

但是,我想添加第二个片locationInfoSlice,它应该在分派addLocation()操作时使用列表中的最后一个位置获取数据.

我怎样才能做到这一点呢?我想大概是extraReducersasyncThunk,但没弄对.

推荐答案

但是,我想添加第二个切片locationInfoSlice,这应该会获取 调度addLocation()操作时的数据,使用最新的 列表中的位置.

如果我对你的帖子/问题的理解是正确的,那么你实际上想要发送some个单一操作,并执行以下操作:

  • state.locations数组中存储一个位置
  • 处理一些异步操作/逻辑,以使用state.locations数组中的最后一个位置获取一些数据.

我怎样才能做到这一点呢?我想大概是extraReducersasyncThunk, 但做得不对.

您可能需要同时使用这两种方法.单靠extraReducers是行不通的,因为Reducer函数是纯同步函数,没有副作用,例如它们不能异步获取数据.我的建议是创建一个Thunk,它调度addLocation操作来更新state.locations状态,并使您需要的数据获取可以返回一个值,该值将用于更新extraReducerslocationInfoSlice‘S状态.

import { createAsyncThunk } from '@reduxjs/toolkit';
import { addLocation } from '../locationsPersistentSlice';

export const addLocationAsync = createAsyncThunk(
  "locations/addLocationAsync",
  async (location, thunkApi) => {
    // dispatch to add new location
    thunkApi.dispatch(addLocation(location));

    try {
      // asynchronous fetch logic
      return data;
    } catch(error) {
      return thunkApi.rejectWithError(error);
    }
  },
);
import { createSlice } from '@reduxjs/toolkit';

const initialState = [];

const locationsPersistentSlice = createSlice({
  name: "locations",
  initialState,
  reducers: {
    addLocation(state, action) {
      state.push(action.payload);
    },
    clearLocations() {
      return initialState;
    },
  },
});

export const {
  addLocation,
  clearLocations,
} = locationsPersistentSlice.actions;

export default locationsPersistentSlice.reducer;
import { createSlice } from '@reduxjs/toolkit';
import { addLocationAsync } from './action';

const initialState = /* whatever this initial state value is */;

const locationInfoSlice = createSlice({
  name: "locationInfo",
  initialState,
  extraReducers: builder => {
    builder
      .addCase(addLocationAsync.fulfilled, (state, action) => {
        // update state with fetched data
      })
      .addCase(addLocationAsync.rejected, (state, action) => {
        // update state with error status???
      })
      .addCase(addLocationAsync.pending, (state, action) => {
        // set any pending/loading state
      });
  },
});

export default locationInfoSlice.reducer;

在用户界面中,它不是直接分派addLocation操作,而是分派addLocationAsync操作.

import { useDispatch } from 'react-redux';
import { addLocationAsync } from './action';

...

const dispatch = useDispatch();

...

dispatch(addLocationAsync(/* some location value */));

...

Javascript相关问答推荐

原型链中的同一财产怎么会有不同的价值?

try 在addEventHandler内设置表单的文件输入.值=空

Angular material 表多个标题行映射

D3多线图显示1线而不是3线

使用AJX发送表单后,$_Post看起来为空

从PWA中的内部存储读取文件

判断表格单元格中是否存在文本框

在vercel throws上部署带有gunjs的sveltekit应用无法找到模块./' lib/文本编码'

用JavaScript复制C#CRC 32生成器

无法访问Vue 3深度监视器中对象数组的特定对象值'

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

使用Promise.All并发解决时,每个promise 的线性时间增加?

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

为什么123[';toString';].long返回1?

基于props 类型的不同props ,根据来自接口的值扩展类型

Webpack在导入前混淆文件名

为什么这个.add.group({})在教程中运行得很好,但在我的游戏中就不行了?

MarkLogic-earch.suggest不返回任何值

从逗号和破折号分隔的给定字符串中查找所有有效的星期几