我需要使用从 api 获取数据的函数设置我的 redux reducer 的初始状态(我使用 supabase 作为我的应用程序的后端)。在我的切片里面,我有这个:
const initialState = {
todoList: []
}
export const saveTodo = createSlice({
name: 'manageList',
initialState,
reducers: {
addTodoToList: (state, action) => {
state.todoList.push({ name: action.payload, isDone: false, isRemoved: false })
},
checkTodoToList: (state, action) => {
state.todoList.map((todo, index) => {
if (index === action.payload) {
if (todo.isDone == true) {
todo.isDone = false
} else {
todo.isDone = true
}
}
return todo
})
},
removeTodoToList: (state, action) => {
state.todoList = state.todoList.filter((todo, index) => index !== action.payload)
}
}
})
现在我想找到最好的方法来设置这个函数返回的空数组:
async function getProfile() {
try {
const user = supabase.auth.user()
let { data, error, status } = await supabase
.from('todos')
.select(`name,isDone,isDeleted`)
.single()
if (error && status !== 406) {
throw error
}
if (data) {
return data
}
} catch (error) {
console.log(error);
} finally {
}
}
在 todoList [] 中设置数据的最佳方法是什么?谢谢