使用createAsyncThunk,createEntityAdapter和本地存储的redux工具包

问题描述

我正在使用redux-toolkit在React Native应用程序中工作,添加对象之前我需要做同样的事情,所以我使用redux toolkit,我的代码片段如下所示:

    ...
createSlice({...
    todoAdded: {
          reducer:todoAdapter.addOne
          prepare:(a,b,c)=> {
            let idx = 1
            a.map(name =>{ name.id = idx,idx = idx+1})
            return {
              payload: {
                d: nanoid(),a: a,c
              },}
          }
        }

但是现在我需要将日期保存在本地存储(AsyncStorage)中,因此我创建了以下函数来保存数据:

export const addTodo = createAsyncThunk('todo/addTodo',async (a,c) => {
    /*
    i do the same thing in the prepare's part of "todoAdded"
    and return object with key,to save it with the setTodo() (function how use AsyncStorage.setItem)
    */
    const response = await setTodo(object,key)
     return response
  }
) 

const todoAdapter = createEntityAdapter({
  selectId: (todo)=> todo.id,})

并将其添加到我的切片的最后

extraReducers: {
    ...
    [addTodo.fulfilled]: todoAdded
  }

因此您可以注意到我有一个重复的代码来准备我的对象,一个用于存储,另一个用于管理数据,我的问题是,处理此用例的最佳实践是什么,是否有优化的解决方案?为两者准备的部分?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)