小编Han*_*Lin的帖子

React createSlice 的 extraReducers 的状态访问

我有以下代码:

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)

reactjs redux react-redux redux-reducers

2
推荐指数
1
解决办法
559
查看次数

标签 统计

react-redux ×1

reactjs ×1

redux ×1

redux-reducers ×1