Functions inside Redux

Hi, I have a doubt on redux . I’ve been using context API in my projects for the state management and avoid prop drilling . Then I decided to learn Redux. I heard that Redux makes state management super easy and a better alternative for context API even though its lil bit tricky in the beginning . But the problem is how could I pass functions to components using Redux. Is it even possible ? Or am I supposed to write those functions inside a seperate module and import it where I need .

@bbsmooth @kevinSmith

Well functions that deal with state are an important part of redux. As far as passing generic functions, you can certainly still pass functions or whatever in the standard way. But in any case, there is no reason why you can’t store a function on redux state. I’vé never needed to since functions that manipulate state are such a foundational aspect of redux.

1 Like

It’s not easy regardless of what you use, because dealing with state is inherently complex, but it’s not that it’s a better alternative. The react bindings for Redux use the Context API, that’s how it works when you use it with React, it’s always worked that way

If you’re putting all your state in one place, then you’d be writing your own version on Redux anyway, so there’s not much point not using it.

Why do you feel you need to do this? What’s the situation where you need to store functions in a state object? Functions are rarely state, so it’s unusual to need to store them in a state object.

As it says in the Redux docs, it’s your application, so you can put whatever you want in the store. But if you do that you can’t

  1. Serialise the store object. As long as it’s just primitive values + objects + arrays, can use JSON.stringify on it and save it in localStorage, then next time the user opens the app they can open it at the same point.
  2. Use time-travel debugging – if the store can be serialised, the redux dev tools extension for the browser can save the state after every action, meaning you can go backwards and forwards through state changes to debug things.
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.