RTKQ, returned data contains Date object, console shows A non-serializable value was…
- User
RTKQ, returned data contains Date object, console shows A non-serializable value was detected in the state, in the path:
farmsApi.queries.getHomePageFarms({"activityType":"fruit","location":{"latitude":-33,"longitude":151},"range":20000,"top":12}).data.results.0.document.farm_activities.0.farm_activity_created_on. Value: Tue May 07 2024 22:22:49 GMT+1000 (Australian Eastern Standard Time) , how to fix? - Assistant
The error message you're seeing is because Redux Toolkit Query (RTKQ) detected that your state contains a non-serializable value, specifically a
Dateobject. Redux enforces the convention of having serializable state to ensure that state updates are predictable and can be easily debugged.To fix this, you should convert the
Dateobject to a serializable format before placing it into the state, and then convert it back to aDateobject when you need to use it. Typically, you can store dates as ISO 8601 strings.Here's how you can handle this in your RTKQ query:
-
Pre-transform the date before saving it into the state:
When you receive data from an API that includes
Dateobjects (such as from yourgetHomePageFarmsquery), you should transform thoseDateobjects to strings.import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; const farmsApi = createApi({ reducerPath: 'farmsApi', baseQuery: fetchBaseQuery({ baseUrl: '/api' }), endpoints: (builder) => ({ getHomePageFarms: builder.query({ query: (params) => ({ url: `/farms`, params, }), transformResponse: (response) => { return { ...response, results: response.results.map(farm => ({ ...farm, farm_activities: farm.farm_activities.map(activity => ({ ...activity, farm_activity_created_on: activity.farm_activity_created_on.toISOString(), // Transform Date to ISO string })), })), }; }, }), }), }); export const { useGetHomePageFarmsQuery } = farmsApi; -
Post-transform the date when using it in your components:
Whenever you need to use that date in your component, you can transform it back to a
Dateobject.import React from 'react'; import { useGetHomePageFarmsQuery } from './farmsApi'; const HomePage = () => { const { data, error, isLoading } = useGetHomePageFarmsQuery({ activityType: 'fruit', location: { latitude: -33, longitude: 151 }, range: 20000, top: 12, }); if (isLoading) return <div>Loading...</div>; if (error) return <div>Error: {error.message}</div>; return ( <div> {data.results.map(farm => ( <div key={farm.id}> <h2>{farm.name}</h2> {farm.farm_activities.map(activity => ( <div key={activity.id}> <p>{activity.name}</p> <p>{new Date(activity.farm_activity_created_on).toString()}</p> {/* Convert string back to Date */} </div> ))} </div> ))} </div> ); }; export default HomePage;
By transforming the
Dateobjects to strings before saving them into the state and converting them back toDateobjects when you need to use them, you ensure that your state remains serializable and you avoid the warning from Redux. -