How to log state.data in reducer function?

Consider the following Redux Toolkit state:

const initialState = {
data: [],
};

const dataSlice = createSlice({
name: "data",
initialState,
reducers: {
setData: (state, action: PayloadAction<DataType[]>) => {
console.log("state.data", state.data);
},
},
});

// state.data Proxy(Object) {type_: 0, scope_: {…}, modified_: false, finalized_: false, assigned_: {…}, …}

I'm unable to log state data in console

Comments

WhimsicalCoder940 2024-12-20 14:34 UTC

If you want to see the state.data you'll have to do console.log as follows:

import { current } from "@reduxjs/toolkit";

console.log("state.data", current(state.data));

reference:

/** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */
declare function current<T>(value: T): T;