我有以下代码:
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { client } from '../../api/client';
const initialState = {
logBook: [],
status: 'idle',
error: null
};
export const logNewEntry = createAsyncThunk(
'logBook/addNewEntry',
async (logEntry) => {
const response = await client.post('/testPost', logEntry);
console.log('post done');
return response.data;
}
);
const logBookSlice = createSlice({
name: 'logBook',
initialState,
// non async calls
reducers: {},
},
// async calls
extraReducers: {
[logNewEntry.fulfilled] : (state, action) => {
console.log('add to list');
console.log(state);
console.log(action);
state.logBook.push(action.payload);
},
}, …Run Code Online (Sandbox Code Playgroud)