Last month, while building a personal file upload component for my project, I hit a wall that left
me scratching my head. I was trying to store a File object directly in my Redux store, and
suddenly my browser console exploded with warnings about "non-serializable data." At first, I
thought it was just Redux being unnecessarily strict, but as I dug deeper, I discovered there were
solid architectural reasons behind this design decision.
This article explores why Redux enforces serialization, the problems non-serializable data causes, and practical workarounds when you absolutely need to handle files, functions, or other complex objects.
The Problem: My File Upload Nightmare
Picture this: I'm building a document management feature where users can upload multiple files, preview them, and submit them with additional metadata. My initial Redux action looked something like this:
/ / โ T h i s s e e m e d l o g i c a l a t f i r s t . . .
c o n s t u p l o a d F i l e = ( f i l e ) = > ( {
t y p e : " U P L O A D _ F I L E " ,
p a y l o a d : {
f i l e : f i l e , / / F i l e o b j e c t f r o m i n p u t e l e m e n t
u p l o a d e d A t : n e w D a t e ( ) ,
i d : M a t h . r a n d o m ( ) ,
} ,
} ) ;
The moment I dispatched this action, Redux DevTools lit up with warnings:
A n o n - s e r i a l i z a b l e v a l u e w a s d e t e c t e d i n a n a c t i o n , i n t h e p a t h : ` p a y l o a d . f i l e ` .
V a l u e : F i l e { n a m e : " d o c u m e n t . p d f " , s i z e : 1 0 2 4 , t y p e : " a p p l i c a t i o n / p d f " }
My first instinct was to ignore itโafter all, the app seemed to work fine. But then I tried using Redux DevTools' time-travel debugging, and everything broke. The file previews disappeared, the upload progress reset, and I realized I had stumbled into a fundamental Redux principle I didn't fully understand.
What Does "Serializable" Actually Mean?
Serialization is the process of converting complex data structures into a format that can be stored, transmitted, or reconstructed later. In JavaScript, this typically means converting objects into JSON strings.
Serializable vs Non-Serializable Data
Serializable data examples:
/ / โ
T h e s e c a n b e s a f e l y c o n v e r t e d t o J S O N
c o n s t s e r i a l i z a b l e = {
s t r i n g : " h e l l o " ,
n u m b e r : 4 2 ,
b o o l e a n : t r u e ,
a r r a y : [ 1 , 2 , 3 ] ,
o b j e c t : { n e s t e d : " v a l u e " } ,
n u l l : n u l l ,
} ;
J S O N . s t r i n g i f y ( s e r i a l i z a b l e ) ; / / W o r k s p e r f e c t l y
Non-serializable data examples:
/ / โ T h e s e c a n n o t b e c o n v e r t e d t o J S O N w i t h o u t l o s i n g i n f o r m a t i o n
c o n s t n o n S e r i a l i z a b l e = {
f i l e : n e w F i l e ( [ " c o n t e n t " ] , " t e s t . t x t " ) ,
d a t e : n e w D a t e ( ) ,
f u n c t i o n : ( ) = > c o n s o l e . l o g ( " h e l l o " ) ,
u n d e f i n e d : u n d e f i n e d ,
s y m b o l : S y m b o l ( " u n i q u e " ) ,
r e g e x : / p a t t e r n / g ,
m a p : n e w M a p ( [ [ " k e y " , " v a l u e " ] ] ) ,
s e t : n e w S e t ( [ 1 , 2 , 3 ] ) ,
} ;
J S O N . s t r i n g i f y ( n o n S e r i a l i z a b l e ) ;
/ / R e s u l t : { " d a t e " : " 2 0 2 5 - 0 6 - 2 8 T 1 0 : 0 0 : 0 0 . 0 0 0 Z " , " r e g e x " : { } , " m a p " : { } , " s e t " : { } }
/ / N o t i c e h o w m u c h i n f o r m a t i o n i s l o s t !
Why Redux Enforces Serialization: The Deep Reasons
After researching Redux's design philosophy, I discovered several compelling reasons for this restriction:
1. Time-Travel Debugging
Redux DevTools' most powerful feature is the ability to "time travel"โreplay actions and inspect state at any point. This requires storing the complete state history.
/ / R e d u x D e v T o o l s n e e d s t o d o t h i s i n t e r n a l l y :
c o n s t s t a t e H i s t o r y = [
J S O N . p a r s e ( J S O N . s t r i n g i f y ( s t a t e 1 ) ) , / / S n a p s h o t a t t i m e 1
J S O N . p a r s e ( J S O N . s t r i n g i f y ( s t a t e 2 ) ) , / / S n a p s h o t a t t i m e 2
J S O N . p a r s e ( J S O N . s t r i n g i f y ( s t a t e 3 ) ) , / / S n a p s h o t a t t i m e 3
] ;
When non-serializable data is involved, these snapshots become incomplete or corrupted. My File
objects turned into empty objects {}, making time-travel debugging useless.
2. Predictable State Mutations
Redux's core principle is that state should be updated immutably. Non-serializable objects often contain mutable properties that can change without dispatching actions:
/ / โ D a n g e r o u s : F i l e o b j e c t s c a n b e m o d i f i e d e x t e r n a l l y
c o n s t s t a t e = {
u p l o a d e d F i l e : s o m e F i l e O b j e c t , / / T h i s o b j e c t m i g h t c h a n g e !
} ;
/ / S o m e w h e r e e l s e i n t h e c o d e :
s o m e F i l e O b j e c t . n a m e = " m o d i f i e d . t x t " ; / / S t a t e c h a n g e d w i t h o u t R e d u x k n o w i n g !
3. Persistence and Hydration
Many Redux applications need to persist state to localStorage or send it over the network. Non-serializable data breaks this completely:
/ / โ T h i s f a i l s s i l e n t l y
l o c a l S t o r a g e . s e t I t e m ( " r e d u x S t a t e " , J S O N . s t r i n g i f y ( s t a t e W i t h F i l e s ) ) ;
/ / L a t e r . . .
c o n s t r e s t o r e d S t a t e = J S O N . p a r s e ( l o c a l S t o r a g e . g e t I t e m ( " r e d u x S t a t e " ) ) ;
/ / F i l e s a r e n o w e m p t y o b j e c t s !
4. Server-Side Rendering (SSR)
In SSR applications, the Redux store needs to be serialized on the server and hydrated on the client. Non-serializable data makes this impossible:
/ / S e r v e r - s i d e
c o n s t i n i t i a l S t a t e = s t o r e . g e t S t a t e ( ) ;
c o n s t s e r i a l i z e d S t a t e = J S O N . s t r i n g i f y ( i n i t i a l S t a t e ) ; / / B r e a k s w i t h n o n - s e r i a l i z a b l e d a t a
/ / C l i e n t - s i d e
c o n s t c l i e n t S t o r e = c r e a t e S t o r e ( r e d u c e r , J S O N . p a r s e ( s e r i a l i z e d S t a t e ) ) ; / / I n c o m p l e t e s t a t e
Real-World Consequences I Discovered
Through my file upload project, I experienced these problems firsthand:
Hot Reloading Breaks
During development, hot reloading would clear my file previews because the File objects couldn't be preserved across reloads.
Redux DevTools Become Useless
I couldn't debug my upload flow because the file data disappeared in the DevTools history.
State Persistence Fails
When I tried to implement "draft" functionality (saving user progress), the files were lost during state rehydration.
Testing Becomes Difficult
Snapshot testing broke because File objects don't serialize consistently across test runs.
Practical Solutions: How to Handle Non-Serializable Data
After understanding the problems, I developed several strategies for working with files and other non-serializable data:
Solution 1: Store References, Not Objects
Instead of storing the actual File object, store a reference and keep the file elsewhere:
/ / โ
S t o r e f i l e m e t a d a t a a n d r e f e r e n c e s
c o n s t u p l o a d F i l e = ( f i l e ) = > {
c o n s t f i l e I d = g e n e r a t e U n i q u e I d ( ) ;
/ / S t o r e f i l e i n a s e p a r a t e m a n a g e r
F i l e M a n a g e r . s t o r e ( f i l e I d , f i l e ) ;
r e t u r n {
t y p e : " U P L O A D _ F I L E " ,
p a y l o a d : {
f i l e I d ,
n a m e : f i l e . n a m e ,
s i z e : f i l e . s i z e ,
t y p e : f i l e . t y p e ,
u p l o a d e d A t : D a t e . n o w ( ) , / / S t o r e t i m e s t a m p i n s t e a d o f D a t e o b j e c t
} ,
} ;
} ;
/ / R e t r i e v e f i l e w h e n n e e d e d
c o n s t g e t F i l e = ( f i l e I d ) = > F i l e M a n a g e r . g e t ( f i l e I d ) ;
Solution 2: Convert to Serializable Formats
For files, convert them to base64 strings or ArrayBuffers (though be mindful of memory usage):
/ / โ
C o n v e r t f i l e t o b a s e 6 4 f o r s m a l l f i l e s
c o n s t c o n v e r t F i l e T o B a s e 6 4 = ( f i l e ) = > {
r e t u r n n e w P r o m i s e ( ( r e s o l v e ) = > {
c o n s t r e a d e r = n e w F i l e R e a d e r ( ) ;
r e a d e r . o n l o a d = ( ) = > r e s o l v e ( r e a d e r . r e s u l t ) ;
r e a d e r . r e a d A s D a t a U R L ( f i l e ) ;
} ) ;
} ;
c o n s t u p l o a d F i l e = a s y n c ( f i l e ) = > {
c o n s t b a s e 6 4 D a t a = a w a i t c o n v e r t F i l e T o B a s e 6 4 ( f i l e ) ;
r e t u r n {
t y p e : " U P L O A D _ F I L E " ,
p a y l o a d : {
n a m e : f i l e . n a m e ,
s i z e : f i l e . s i z e ,
t y p e : f i l e . t y p e ,
d a t a : b a s e 6 4 D a t a , / / N o w s e r i a l i z a b l e !
} ,
} ;
} ;
Solution 3: Use Redux Middleware to Filter Non-Serializable Data
Redux Toolkit provides middleware that can automatically handle or warn about non-serializable data:
i m p o r t { c o n f i g u r e S t o r e } f r o m " @ r e d u x j s / t o o l k i t " ;
c o n s t s t o r e = c o n f i g u r e S t o r e ( {
r e d u c e r : r o o t R e d u c e r ,
m i d d l e w a r e : ( g e t D e f a u l t M i d d l e w a r e ) = >
g e t D e f a u l t M i d d l e w a r e ( {
s e r i a l i z a b l e C h e c k : {
i g n o r e d A c t i o n s : [ " f i l e / u p l o a d " ] , / / I g n o r e s p e c i f i c a c t i o n s
i g n o r e d P a t h s : [ " f i l e s . t e m p U p l o a d s " ] , / / I g n o r e s p e c i f i c s t a t e p a t h s
/ / O r d i s a b l e e n t i r e l y ( N O T r e c o m m e n d e d )
/ / s e r i a l i z a b l e C h e c k : f a l s e
} ,
} ) ,
} ) ;
Solution 4: Separate Non-Serializable State
Keep non-serializable data in React component state or context, and only store serializable metadata in Redux:
/ / C o m p o n e n t h a n d l e s f i l e o b j e c t s
c o n s t F i l e U p l o a d = ( ) = > {
c o n s t [ f i l e s , s e t F i l e s ] = u s e S t a t e ( [ ] ) ; / / N o n - s e r i a l i z a b l e f i l e s s t a y h e r e
c o n s t d i s p a t c h = u s e D i s p a t c h ( ) ;
c o n s t h a n d l e U p l o a d = ( f i l e ) = > {
s e t F i l e s ( ( p r e v ) = > [ . . . p r e v , f i l e ] ) ; / / S t o r e f i l e i n c o m p o n e n t s t a t e
d i s p a t c h (
a d d F i l e M e t a d a t a ( {
/ / O n l y m e t a d a t a g o e s t o R e d u x
i d : g e n e r a t e I d ( ) ,
n a m e : f i l e . n a m e ,
s i z e : f i l e . s i z e ,
s t a t u s : " u p l o a d i n g " ,
} )
) ;
} ;
/ / . . . r e s t o f c o m p o n e n t
} ;
When Is It OK to Break the Rules?
Sometimes, you might need to bypass Redux's serialization requirements. Here are scenarios where it might be acceptable:
Temporary UI State
/ / F o r s h o r t - l i v e d , n o n - c r i t i c a l d a t a
c o n s t s t o r e = c o n f i g u r e S t o r e ( {
r e d u c e r : r o o t R e d u c e r ,
m i d d l e w a r e : ( g e t D e f a u l t M i d d l e w a r e ) = >
g e t D e f a u l t M i d d l e w a r e ( {
s e r i a l i z a b l e C h e c k : {
i g n o r e d P a t h s : [ " u i . t e m p F i l e s " ] , / / O n l y i g n o r e s p e c i f i c p a t h s
} ,
} ) ,
} ) ;
Development-Only Features
/ / D i s a b l e c h e c k s o n l y i n d e v e l o p m e n t
c o n s t s t o r e = c o n f i g u r e S t o r e ( {
r e d u c e r : r o o t R e d u c e r ,
m i d d l e w a r e : ( g e t D e f a u l t M i d d l e w a r e ) = >
g e t D e f a u l t M i d d l e w a r e ( {
s e r i a l i z a b l e C h e c k : p r o c e s s . e n v . N O D E _ E N V = = = " p r o d u c t i o n " ,
} ) ,
} ) ;
Key Takeaways
Redux's serialization requirement isn't arbitraryโit's fundamental to its debugging, persistence, and predictability features. While it initially seemed like a roadblock in my file upload project, understanding the reasoning led me to build a more robust, maintainable solution.
The main lessons I learned:
- Respect the architecture: Redux's constraints exist for good reasons
- Separate concerns: Keep non-serializable data separate from your Redux state
- Store references, not objects: Use IDs and external managers for complex data
- Convert when possible: Transform non-serializable data into serializable formats
- Break rules carefully: If you must bypass serialization, do it selectively and document why
Understanding these principles has made me a better Redux developer and helped me build more maintainable applications. The next time you encounter that serialization warning, don't ignore itโembrace it as an opportunity to build better architecture! ๐