Recoil: control equality checks or provide imperative return values

Created on 22 May 2020  路  3Comments  路  Source: facebookexperimental/Recoil

I notice that there is no way to control equality checks on returned values, e.g. from selectors "get" definition.

Is there a way to definitely control equality checking? Something like the comparator in React.memo. I'd like to be able to tell a selector what "change" means in on return value. I guess I could create a return loop to get the previous value, but that's seems awkward or I could break up my state objects and expand the graph further to separate out the pieces. I noticed "constSelector" but its not clear that I can return from a selector to indicate that something should be const without having the previous value (which requires a feedback loop in the graph).

enhancement

Most helpful comment

Yes, as @acutmore pointed out, we have plans for a proper API to control the propagation of updates. In the meantime, if you're adventurous and don't mind an evolving API and need to unblock yourself, you can play with what we currently use under the cacheImplementation selector options. These control the equality on inputs, vs outputs as you describe. I expect the final API for this will be very different and closer to what you're requesting.

All 3 comments

Hi @aappddeevv. It looks like the Recoil team do plan to have an API similar to this:

Internally, we're working on a mechanism to limit updates when the values have not changed. But, we're planning to really clean up that API before publishing it.
https://github.com/facebookexperimental/Recoil/issues/41#issuecomment-629601674

Thanks for pointing that out, I did not gleam that it was related to this one.

Equality control is pretty important so as long is it can be explicit I'm happy. The issue is also that with non-javascript flavored transpiled languages object equality becomes even more tricky sometimes.

Yes, as @acutmore pointed out, we have plans for a proper API to control the propagation of updates. In the meantime, if you're adventurous and don't mind an evolving API and need to unblock yourself, you can play with what we currently use under the cacheImplementation selector options. These control the equality on inputs, vs outputs as you describe. I expect the final API for this will be very different and closer to what you're requesting.

Was this page helpful?
0 / 5 - 0 ratings