IT이야기

ResactRedx 툴킷 상태에서 어레이가 정의되지 않은 이유

cyworld 2022. 3. 12. 10:27
반응형

ResactRedx 툴킷 상태에서 어레이가 정의되지 않은 이유

console.log(배열)를 @relementxjs/proxkit로 관리하려고 할 때 항상 정의되지 않거나 프록시가 된다.

사용할 때import { original } from "immer";그리고console.log(original(state.theArrayImTryingToLog));나는 단지 정의가 없다는 것을 알게 되었다.

사용할 때console.log(state.theArrayImTryingToLog);I get: 0: Proxy [[Handler]: null [Target]: null [IsReviruated]: true length: 1 배열이 처음에 빈 배열로 정의되었을 때 또는 더미 객체를 설정했을 때에도 기록된다.

나는 다른 게시물들에서 이것이 임머의 문제일 수도 있다고 읽었지만, 나는 항상 정의되지 않은/대리를 받기 때문에 그것이 다른 것일 수도 있다고 의심한다.

또한 언제console.log(JSON.stringify(state.theArrayImTryingToLog));그 모든 요소들과 함께 정확한 배열을 얻는다.

import { createSlice } from "@reduxjs/toolkit";
import { getProducts } from "../api/product.api";
import { original } from "immer";

const productsSlice = createSlice({
  name: "products",
  initialState: {
    products_in_cart: [],

  },
  reducers: {
       add_to_cart: (state, action) => {
      console.log(state.products_in_cart); -> proxy as above
      console.log(original(state.products_in_cart)); -> undefined
      console.log(JSON.stringify(state.products_in_cart)); -> correct array as string
    },
  },
});

참조URL: https://stackoverflow.com/questions/62852730/why-an-array-is-undefined-in-state-react-redux-toolkit

반응형