redux 错误-在状态中检测到不可序列化的值,路径为:'产品,价值:{"_x”:0”_y”,_z”:空,"_A”:空}

w3nuxt5m  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(100)

我基本上是试图保存喜欢/不喜欢的状态的产品列表,我试图从API获取使用axios,但我得到下面的错误.
在状态中检测到不可序列化的值,路径为:'产品。价值:{"_x ":0"_y ",_z":空,"_A":空}
为了保存状态,我尝试使用Redux工具包。下面我有productSlice组件和ProductListScreen组件的代码。
productSlice.js

import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const productsSlice = createSlice({
  name: "products",
  initialState: { items: [], likes: [] },
  reducers: {
    fetchProducts: async (state) => {
      const response = await axios.get("https://dummyjson.com/products");
      state.items = response.data;
      state.likes = new Array(state.items.length).fill(false);
    },
    toggleLike: (state, action) => {
      state.likes[action.payload.index] = action.payload.isLiked;
    },
  },
});

export const { fetchProducts, toggleLike } = productsSlice.actions;

export default productsSlice.reducer;

productListScreen.js

import { useEffect, useState } from "react";
import { FlatList, Text, View } from "react-native";
import { useDispatch, useSelector } from "react-redux";
import { fetchProducts, toggleLike } from "./productsSlice";

const ProductListScreen = () => {
  const products = useSelector((state) => state.products.items);
  const likes = useSelector((state) => state.products.likes);
  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(fetchProducts());
  }, []);

  const handleLike = (index, isLiked) => {
    dispatch(toggleLike({ index, isLiked }));
  };

  const renderItem = ({ item, index }) => (
    <View>
      <Text>{item.title}</Text>
      <LikeDislikeButton
        productId={item.id}
        liked={likes[index]}
        onPress={() => handleLike(index, !likes[index])}
      />
    </View>
  );

  return (
    <View>
      <FlatList
        data={products}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
};

export default ProductListScreen;
deikduxw

deikduxw1#

Reducer函数应该是纯***同步***函数。您已经声明了fetchProducts Reducer函数async,它 * 隐式 * 返回Promise对象。这是存储到state.products中的不可序列化对象。
您应该创建和使用异步操作(即Thunk)来进行异步API调用。将fetchProducts重构为异步操作。使用productSliceextraReducers来处理从fetchProducts thunk返回的已履行承诺。

import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import axios from "axios";

export const fetchProducts = createAsyncThunk(
  "products/fetchProducts",
  async () => {
    const response = await axios.get("https://dummyjson.com/products");
    return response.data;
  },
);

const productsSlice = createSlice({
  name: "products",
  initialState: { items: [], likes: [] },
  reducers: {
    toggleLike: (state, action) => {
      state.likes[action.payload.index] = action.payload.isLiked;
    },
  },
  extraReducers: builder => {
    builder
      .addCase(fetchProducts.fulfilled, (state, action) => {
        const { products } = action.payload;
        state.items = products;
        state.likes = new Array(products.length).fill(false);
      });
  },
});

export const { toggleLike } = productsSlice.actions;

export default productsSlice.reducer;
gdrx4gfi

gdrx4gfi2#

response.data是包含产品列表的对象。

{"products":[{"id":1,"title":"iPhone 9",...}]}
const response = await axios.get("https://dummyjson.com/products");
state.items = response.data.products;

相关问题