Computed derived data access from multiple components
Hey there, I'm coming to this project from baobab, and one of the features I like best about baobab is the ability to compute derived data from the state tree and store it in the state tree.
Here's an example of what that would look like in baobab:
{
foo: 'bar',
baz: 'yip'
derivedNode: deriveData(['foo'], ['baz'], function(foo, baz) {
return foo + baz
})
}
In my components I could then access 'derivedNode' like any other data within the tree. This would work well if I needed that data across multiple top level components.
I read the docs section about the memoized selector function, which seems like a nice solution for generating derived data, but I was wondering what to do if you needed the derived data across multple "smart" components. Do they each have to implement the same selector function on the connect call?
Just started checking out this library today so I hope my question makes sense.
While I'm here... Another quick question I have, it seems like for every reducer added to the store, another set of action type strings need to be checked against. Does this method ever encounter issues with scale? Once many actions are being fired, it seems like it might be time consuming to compute all those switch statements. Has this ever been an issue for anyone?
Thanks very much, Thomas
Defining a memoized selector function in a module and importing it into different smart components should work fine. You can open an issue over at the Reselect repo if you have any problems and I'll do my best to help.
Won't the selector function be duplicating work for every module it is attached to? Also what about the little part 2 of my question concerning action type checking?
On Fri, Sep 25, 2015, 1:48 AM ellbee [email protected] wrote:
Closed #800 https://github.com/rackt/redux/issues/800.
— Reply to this email directly or view it on GitHub https://github.com/rackt/redux/issues/800#event-418949566.
No, it won't duplicate work (assuming the selector is being used as the mapStateToProps argument to Connect).
Action type checking with switch statements is the standard approach among flux implementations. I have never heard of it being a performance problem.
@tnrich I have fleshed out the answer to this (it applies in the common case but there are caveats) in the Reselect FAQ.
Awesome, thanks @ellbee! Much appreciated.