JoeTidee
JoeTidee

Reputation: 26124

How to save a function to a Redux store?

I would like to save a function (reference) to my Redux store so that I can:

  1. pass the function references to components that are connected to the store state, or
  2. retreive a stored function and run it within an action creator.

The general consensus seems to be that you should not store function references in the Redux store (because the data should be serializable). Are there any ways to get around this so that the above two processes can be realised?

Upvotes: 3

Views: 5211

Answers (1)

markerikson
markerikson

Reputation: 67607

Per the Redux FAQ entry on non-serializable items in state:

It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to insert non-serializable items into the store, but doing so can break the ability to persist and rehydrate the contents of a store, as well as interfere with time-travel debugging.

If you are okay with things like persistence and time-travel debugging potentially not working as intended, then you are totally welcome to put non-serializable items into your Redux store. Ultimately, it's your application, and how you implement it is up to you. As with many other things about Redux, just be sure you understand what tradeoffs are involved.

Upvotes: 3

Related Questions