Redux: Can I store a file (image, png...) in redux store?

Created on 3 Mar 2017  Â·  9Comments  Â·  Source: reduxjs/redux

I am currently using dropzone library to upload files:
screen shot 2017-03-03 at 4 47 15 pm

I want to save the file in redux for later save to db.

I can read the file I uploaded in the dropzone accept function, but once I save it in redux and refresh the page (I use redux-persist to retain the data), the File becomes an Object, and I could not continue to save it to my server on save button click.

Here is the example:
screen shot 2017-03-03 at 4 59 30 pm

I am not sure if it is a redux issue or redux-persist issue, does redux allow File to be save in the store?

Most helpful comment

That is neither redux, nor redux-persist issue. That is a limitation of the web platform. You cannot persist objects other than serializable (plain) objects between page reloads.

All 9 comments

That is neither redux, nor redux-persist issue. That is a limitation of the web platform. You cannot persist objects other than serializable (plain) objects between page reloads.

However, I think you can store the base64 version of it (it's kinda big, but could work). But yeah, not a limitation of redux, and Dan always said the store should be serializable

You _can_ store a non-serializable item in the store, but it's usually not the best idea. See this FAQ entry: http://redux.js.org/docs/faq/OrganizingState.html#organizing-state-non-serializable .

And yes, I wouldn't expect File objects to be persistable by the browser.

you can persist file with indexedDB though (at least with chrome as far as i can remember).
(old) example using Dexie https://github.com/viankakrisna/materialplayer/blob/master/assets/js/mp.db.js

@sompylasar @asantos00 @markerikson @viankakrisna Thank you so much for the prompt reply! I still have a question.

Since we cannot save the non-serializable object to store, what's the common practice for people that wants to upload images first and confirm save it later on save button click?

@ZhuQinglei i don't have an answer to this 😅 Also, it's a usage question. You should probably better ask it on Stack Overflow.

I think here is definitely not the right place to ask it. However, I think you can upload the images and store a server reference for them. Imagine you upload an image, the server returns you the uploadedImageId and then you send it later when you save it.
At least I've done it like this

@asantos00 Yes, but what if you'd want to achieve this with a single request?

i think you can stall the name of the file in redux as:

initialState:{
userImage: 'name.png',
}

and pass it as props to the image tag as

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elado picture elado  Â·  3Comments

caojinli picture caojinli  Â·  3Comments

mickeyreiss-visor picture mickeyreiss-visor  Â·  3Comments

jbri7357 picture jbri7357  Â·  3Comments

benoneal picture benoneal  Â·  3Comments