I was hoping to achieve with this library something similar to what's possible with uncontrollable: a component that self-manages its own state, say an <input type="text" {...} />, but that also allows its parent to control it, if the value (and possible the onChange) prop is given.
This is sort of possible, according to what's pointed out here, but I think that doesn't solve the issue completely, for a couple of reasons:
onChange), but rather the presence or not of the value. If the value is given as a prop from the outside, that should be the thing that triggers the component to relinquish control.mapProps which one to pass.defaultValue in the example.My question is: is it possible to achieve this (at least points 1 and 2 above) using primitives from this library alone, without the caveats mentioned about the example on #7? And if not, wouldn't it make sense to add it, either improving some of the primitives, or providing a new one?
Follow up question: I'm pretty certain that the point 3 above is not achievable, because it seems a more ad-hoc thing to be done in order to sort of match that defaultX is meant to initialize state.x. Would this be something that makes sense to add to this library as well?
PS: I'm more than happy to work on a PR myself if anything of what I propose makes sense.
usually branch + withStateHandlers is enough.
branch(
// as value can be empty better to check that no handler passed
({ onChange }) => !onChange,
withStateHandlers(
({ defaultValue }) => ({ value: defaultValue}),
onChange: () => value => ({ value })
)
)
Wow great! It certainly is possible with the primitives already in place. Thanks!
I'd still use it (as I am doing in my case right now) with the criteria being value === undefined, instead of using the presence of the onChange handler. I think is more precise. Otherwise, it seems unintuitive that the outside world gives my component a value and no onChange, and my component continues to control itself, when the presence of the value prop suggest otherwise.
Anyway, thanks. This library is more awesome each day I use it.
Most helpful comment
usually
branch+withStateHandlersis enough.