-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: handling derived data #47
Comments
Can't this be already achieved with the The only missing piece would be caching the getters during the dispatch cycle, as Nuclear does. Here perhaps is where we can use what you're suggesting with the concept of Or perhaps this last thing can be just plain old function memoization? |
Has there been any more thought on this? This area is interesting to me as well. I'm also curious what injectors are, it seems like maybe that was an older concept in redux? |
I think the way to go here is to provide utilities for creating smart memoizing |
@gaearon After your suggestion in this gist https://gist.github.com/gaearon/d77ca812015c0356654f about combining stores/reducers we decided to use selectors to derive data from one or multiple stores/reducers. Due to performance considerations we drafted an initial selector function which can be used to create slectors and memoize the results. Below is the initial draft, which is applied to the example of todos an their derived count. In the next days we will try to smooth out the rough edges and provide the selector create function and examples if this sounds interesting to you. selector = (...keys, transformationFunction=_.identity) => {
let lastParams = {};
let lastResult = null
return (state) => {
if(!lastResult || _.find(keys, (key) => lastParams[key] != state[key] ) {
lastParams = _.pick(subState, keys);
lastResult = transformationFunction(lastParams);
}
return lastResult;
}
} An example module providing selectors related to the todos store/reducer would look like this: export let todoSelector = selector('todos');
export let todoCountSelector = selector('todos', { todos } => {
return {
count: todos.length
};
}); |
Great! Have you considered a composable API similar to NuclearJS's getters? I think they really nailed it. |
Created Proposal for selectors: #169 |
This is now being solved outside Redux: https://github.com/faassen/reselect |
@matystl brings up a great point here:
AFAIK NuclearJS solves this with the concept of Getters.
I don't want to complicate Redux with first-class support for derived data, but it's nice to consider options. Ideally I want something like this as a plugin for Redux. (It could come with a custom
Injector
that binds to a specific “getter”.)There's no implementation plans at this point, but I'd love to discuss this and see your ideas!
The text was updated successfully, but these errors were encountered: