v3 breaks my app
I'm not sure what's happening. I (ab)use reselect to calculate derived values from component props and state, by using my React element as state, not simply the redux store.
Example:
class Table extends React.Component {
getColumns = createSelector(
[
() => this.props.columns,
() => this.state.show,
],
(origCols = {}, show) => {
// calculate the column object to give to the table component
}
)
render() {
const allCols = this.getColumns()
// …
}
}
I am passing extra data (via deep mutation) through the columns object to the Table component, and to make sure the columns are recalculated, I copy the column object into a new one. That works fine with v2.5.4, but in v3 the selector doesn't update.
Is there a deeper comparison of the inputselectors being made in v3? The columns object changes but its direct descendants don't.
I suppose I should just man up and rewrite that code so the mutations are an extra input, but it would be good if this is noted as a possibly breaking change.
Oh, sorry! I wasn't aware of any behaviour change, our tests didn't pick it up. Would it be possible to knock up a quick test case which demonstrates the difference between the versions?
👋
This broke right here: https://github.com/reactjs/reselect/pull/194/files#diff-1fdf421c05c1140f6d71444ea2b27638R69
I assumed that it would be ok since reselect was built for redux and if you're mutating the state in the redux then you won't even get to the point where you're calling selectors to get values out of the state since nothing is going to know it needs to update.
However you're calling reselect outside of redux and just in react which does support mutation. If mutation is something that reselect should support then it should definitely be something that is explicitly enabled. I do feel that this does go against the spirit of the library itself and the library it's built for.
But yes, I think that at the very least we should update the CHANGELOG to show this has changed from v2-v3.
I see three easy options:
- keep the old behavior on
createSelectorand add acreatePureSelector - break the old behavior and add
createImpureSelector - stop supporting the old behavior
One thing to note though, IIRC when redux calls its subscribers, the store object has always changed, so this optimization won't help.
On Sun, Mar 26, 2017, 6:35 PM John Haley [email protected] wrote:
👋
This broke right here: https://github.com/reactjs/reselect/pull/194/files#diff-1fdf421c05c1140f6d71444ea2b27638R69
I assumed that it would be ok since reselect was built for redux and if you're mutating the state in the redux then you won't even get to the point where you're calling selectors to get values out of the state since nothing is going to know it needs to update.
However you're calling reselect outside of redux and just in react which does support mutation. If mutation is something that reselect should support then it should definitely be something that is explicitly enabled. I do feel that this does go against the spirit of the library itself and the library it's built for.
But yes, I think that at the very least we should update the CHANGELOG to show this has changed from v2-v3.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/reactjs/reselect/issues/231#issuecomment-289295575, or mute the thread https://github.com/notifications/unsubscribe-auth/AADWlhDbiIV0kSmN7lL_JmXHA-gSBSWiks5rppPSgaJpZM4MpB_T .
Not exactly true. combineReducers will return the same root state object if it thinks all of the slice reducers returned their previous slices of state untouched.
The biggest benefit is if you have a selector used as a root selector that is called multiple times from other selectors per state update. Instead of determining that a referentially equal state has a change, if you use redux as it's designed and consider the state immutable you only have to run that calculation once for any given state update.
I guess that line is the cause of #233 too.
Edit: Never mind, this actually seems to be working after a quick test.
I think @johnhaley81 is right in that Reselect was intended to be used with immutable data. The 'impure' use case was accidentally supported. I am trying to see if there is a nice way to have the optimization as the default, but allow the old behavior too. Suggestions welcome.
By nice, I mean preferably with no breaking changes and the most minimal introduction of top level API possible (I'm not keen on trying to write the docs for createImpureSelector!)
Until we reach a conclusion on what we want to do here, I've added a breaking changes section to the release notes.
Well, no breaking changes means making the initial state comparison
optional, and adding a third argument to createSelector for backwards
compatible API createSelector(inputSelectors, compute, isPure).
On Mon, Mar 27, 2017 at 5:34 PM Lee Bannard [email protected] wrote:
By nice, I mean preferably with no breaking changes and the most minimal introduction of top level API possible (I'm not keen on trying to write the docs for createImpureSelector!)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/reactjs/reselect/issues/231#issuecomment-289420860, or mute the thread https://github.com/notifications/unsubscribe-auth/AADWllimaWV8tUuTVMNjJSvdBQwdv96Kks5rp5ZsgaJpZM4MpB_T .
The problem with allowing the grey case of mutable data is that it could be confusing for the user since reselect would be the exception where everything else that's built in redux land assumes immutability.
And that exception would also not be universal in reselect. The root state object is mutable but all subvalues are not. I guess the main difference is that you could override the underlying equality function but not the equality function for the top level state === state. That could be a start if this is to be implemented.
However, by default everything assumes immutability and that's why it can do things so fast. By falsifying that assumption at any level you then must full verify things haven't changed for each call which can really slow down root level selectors that are called multiple times for any given state change.
Reading over the above example however, I feel like using a selector inside the render would be sub-optimal.
Think about it this way, when you're inside of the render, if you're doing potentially expensive calculations then you should either short circuit that inside of that component's shouldComponentUpdate or pass that slice of your component's prop and state (i.e. this.props.columns and this.state.show) down to a subcomponent that would then do the prop equality check inside of shouldComponentUpdate. And then React could optimize that DOM update away.
Using the internal lifecycle methods of React that are built for what you're trying to do seems a lot more sane to me.
With all of that being said, I'm really sorry my PR broke your code :(
Well, no breaking changes means making the initial state comparison optional, and adding a third argument to createSelector for backwards compatible API
createSelector(inputSelectors, compute, isPure).
v3.0.0 has been released now though, so I'm thinking a breaking change would be to return to the v2.5.4 behavior if we are following semver.
I agree with the points @johnhaley81 made, and I'd also add that it complicates the already complicated docs if we have to make clear that although it is supported we don't necessarily recommend the impure case. On the other hand I don't really want to break a load of peoples apps without an upgrade path.
So, any thoughts on this in the meantime?
For me, the solution would be to only check the state for equality if the state is not undefined. Then it will run my bound input selectors and everything will work as it should. This is entirely backwards compatible.
More generically, I think adding an option parameter to createSelector, or making a different createNonPureSelector would provide an easy upgrade path for people that are relying on the unintended v2 behavior.
@wmertens if you're looking for a solution in the meantime, this should work. Obviously doesn't work for use in @connect(), but is a convenient way to avoid the === check for your use case.
function createClassSelector(...args) {
const selector = createSelector(...args);
const wrapper = () => selector({});
return Object.assign(wrapper, selector);
}
I also got bit by this.
Solved it with the following workaround that should be fine unless your input functions are using the argument count or otherwise can't handle an extra argument
export const createNonPureSelector = (...args) => {
const selector = createSelector(...args);
return (...args) => selector(...args, {});
};
I ran into the same limitation as well.
In my case, I was trying to use reselect as an incremental migration path for non-redux-based code: to initially calculate derived values from mutable state, using the same API you would eventually use with immutable state.