in Redux RTK getting " non-serializable value was detected in an action" for a Date?
Asked Answered
K

1

5

With Redux RTK I'm getting this error "non-serializable value was detected in an action", however I just have Date's here, and they are serializable right? So wondering why this is?

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Kwasi answered 29/7, 2022 at 0:55 Comment(0)
P
8

Redux does not serialize objects for you

You are currently putting the class instance of the date so it's not serializable (because it's the class instance) however you can serialize a Date in Javascript

Are dates serializable?

You can call new Date().toISOString()

Which will produce a string in the store

Pedagogy answered 29/7, 2022 at 7:56 Comment(4)
actually I'm not sure what you mean by "class instance of the date"? When I try this a date seems to serialize ok: const dateVar = new Date() console.log("Raw Date: ", dateVar) console.log("Sringified: ", JSON.stringify(dateVar));Kwasi
I do not think redux is calling JSON.stringify for youPedagogy
This is why you have to do toISOString(), JSON.stringify, toJSON etc... to serialize your datePedagogy
github.com/reduxjs/redux/search?q=JSON.stringifyPedagogy

© 2022 - 2024 — McMap. All rights reserved.