Home > Blockchain >  Problem updating a list with redux toolkit
Problem updating a list with redux toolkit

Time:10-23

// './file_1'

import { createSlice } from '@reduxjs/toolkit';

export const array = createSlice({
    name: 'array',
    initialState: {
      value: []
    },
    reducers:{
      append(state, a) {
        state.value.push(a)
      }
    }
});

export const { append } = array.actions;
export default array.reducer;

I've also updated the store, so no problem on that side

'/.file_2'
import { useSelector, useDispatch } from 'react-redux';
import { append } from './file_1';
const dispatch = useDispatch()
let x = 5
dispatch(append(x));

raises this error: Objects are not valid as a React child (found: object with keys {type, payload}). If you meant to render a collection of children, use an array instead.

CodePudding user response:

"a" is an object. The object looks like this -> {action: ..., payload: ...} so you need to access it like this -> a.payload

  • Related