Denis  Yarkovoy
Denis Yarkovoy

Reputation: 1307

References in redux store

Is there a common approach to store serializable references to non-serializable objects in redux state tree?

The simple use case is uploading a [rather big] file to the server from reactjs client. If such upload is a part of form submission algorithm driven by redux actions, we need to keep a reference to the file in the store. The simplest approach would be to keep the entire contents of the file in the store, but this is inefficient for large files.

One of the ways I'm thinking is to introduce a simple map of {stringkey:Blob} and keep stringkey in the state tree. Though in this case we need to implement reference counting or something else to delete unused keys.

Is there a better way?

Upvotes: 1

Views: 1019

Answers (1)

Mark Williams
Mark Williams

Reputation: 2308

I'd steer away from using the store to, er, store large files. It maintains the state for your application, so a reference to the file should be all that is required (incidentally, it is good practice to keep the store normalized, along the lines of database practice).

I'd keep the side affect of uploading the file away from the reducer logic. You can do this with middleware in redux so that the file is uploaded as part of an action; on success a subsequent action updates the store with whatever is needed for subsequent rendering, application state etc. The actual file (if it is needed) can be stored elsewhere and merely referenced from your store.

Upvotes: 2

Related Questions