我有以下代码:
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);
},
},
})
//export const {resetName} = logBookSlice.actions;
export default logBookSlice.reducer;
export const selectLogBook = (state) => state.logBook.logBook;
没有引用日志的console.log(state);
状态,因此我无法向其中添加新条目。什么控制台打印:
Proxy {i: 0, A: {…}, P: false, I: false, D: {…}, …}
[[Handler]]: null
[[Target]]: null
[[IsRevoked]]: true
我以减少模板的 counterSlice 为例来构建它,以及他们的作品。
incrementByAmount: (state, action) => {
state.value += action.payload;
},
我究竟做错了什么?