Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Reducer querying #943
Is it possible to query reducers, such as for determining whether an async action needs to run or not?
If not, I would like to propose a discussion on the possibility of introducing them. These queries would be their own action type with a payload, except a reducer returns a response instead of the entire state.
They would be polymorphic – not coupled to any particular reducer. This allows more flexible code, rather than having actions having specific knowledge about the structure of the state tree that
Note this is also distinct from the helper functions that I have seen in examples, where alongside a reducer, other functions are exported to extract data from its state. These are coupled to a particular reducer.
I have implemented a system like this in my Redux additions library Flambeau (see https://github.com/BurntCaramel/flambeau/blob/master/docs/reducers.md#introspection), but it would be great to have similar functionality in Redux.
Even just the bare minimum of sending arbitrary messages to reducers without affecting state, and then the rest could be built on top as a plugin?
A reducer in Redux is just a function to return new state. It can't return any other value except the new state. It's fundamental to how Redux works.
Your action creator though, can certainly 'query' the state to determine whether to start an async action. Exactly how that works will depend on whether you are using plain objects or Immutable or Updeep, etc., but the state is available to your action creator.
I was going to suggest you look at the
What exactly is it in the
Yes I like that system of selector functions – they are sort of like a public interface, that allows the structure of the state to be encapsulated. I ported your Redux example to use my library Flambeau (sorry I hope you are not sick of me talking about it) – you can see where I’ve 'queried' reducers here with its own syntax (
It’s different in that a selector is tied to a particular reducer. And, that is tied to knowing the position of the reduced data in the state tree.
This couples the async action creator to a particular reducer. Essentially removing some of the benefits of decoupling that Flux allows. It means that the async action creator can’t be reused anywhere, say in another part of the project, or another project all together. And the reducer can’t be moved around the state tree, without having to go in and change the async action creator too.
Ideally, I think, the async action creator should have no knowledge of the state tree, of reducers at all. All it does is send actions, and what I am proposing is a way for it to also receive, in a way that it defines. The action creators define the action types and the query types, and it’s up to the reducers to respond back.
That’s cool. Is it ok if I propose other small changes that would make such a thing more natural? Currently it relies on a reference to the root reducer, as that is what it queries. However, there is no way of accessing this outside of where myself or another user declare the root reducer, as the store API only allows setting a root reducer, not getting it.
I understand it is encapsulated for a reason, so that the scope of Redux’s store is as small as possible. However, could there be a way of accessing the root reducer, say in react-redux’s
I could create a store enhancer to do this myself, e.g. add a
Anyway, just a proposal, I’m not sure if there are other issues that would benefit from this. Appreciate the work on Redux, and would like to help in some other ways too than a rambling issue, if possible.