-
Notifications
You must be signed in to change notification settings - Fork 101
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
add reset/resetState
?
#110
Comments
reset/resetState
to <State>
?reset/resetState
?
Thanks @ianstormtaylor 😄 I like that reset api. To State Containers I think it makes sense. The reason for the setState to be different from the others, is that users are already comfortable with state / setState of a common component. In fact, State's setState works exactly like the setState of a Stateful Component. Before (0.1x), the set functions were more descriptive I thought that if I kept the same prop for all ( PS: I hate the fact that Value has a value state, List has a list state, but Toggle has |
Here's how I usually use things <Toggle>
{drawer =>
<>
<button onClick={drawer.toggle}>{drawer.on ? 'close' : 'open'}</button>
{drawer.on && <Drawer onClose={() => drawer.set(false)} />}
</>
}
</Toggle> Unified fields work good with specific containers. State container in opposite is generic and can't have specific name. I use it always like this <State initial={{ on: false }}>
{({ state, setState }) =>
<>
<button onClick={() => setState({ on: true })}>open</button>
{state.on && <Drawer onClose={() => setState({ on: false })} />}
</>
}
</State> |
Thanks for your point @TrySound! |
I'm not sure. It can be implemented easy by hoisting initial value. However this value can be changed with a time and I'm not sure what is the right thing to set the new one or the first one. It depends on user requirements. I didn't have such use case tbh. It looks too opinionated. |
How is to "too opinionated"? It's a nice convenience that the library can make extremely easy with minimal logic, or everyone can reinvent themselves with the logic spread out over many places. I think anything that exposes Otherwise resetting the state will always involve either duplicating the |
@renatorib that makes sense about the The way I'd thinking about it is that it's good that everything is always Edit: For what it's worth, the |
Aliases are a bad for api design. It means we can't decide how feature should be used. There should be only one way to do things. |
I agree with @TrySound about aliases things. I'm resilient about creating aliases. I'm not going to make a decision now, but I'm really thinking of implementing the Anyway, I'll leave this issue open until I decide what to do! |
FWIW, this tweet kind of sums up the reasoning behind my alias argument: https://twitter.com/sebmarkbage/status/1005162462486900737 I still think that every component should expose By calling it But at the same time, while programming, people familiar with React are going to naturally mis-remember the Of course, I also agree that many different ways to do things is not good. But in this case, I think it's really just exposing a single "thing" in both a canonical way, for consistency, and in a "guessable" way, to avoid frustration. Anyways, food for thought. |
IMO reset is needed, imagine following (of course bad ;-)) code. ({someProp}) =>
<Value initial={someProp}>{
val => <Button onClick={() => someAsync(() => {
// at the moment this callback is called, someProp is already has changed,
// and I need to reset Value to it's initial, but calling
// val.set(someProp) isn't same as reset, as callback contains previous version of someProp
// so val.reset() is needed
}) } here is simple reproducible example of a problem, even without async https://codesandbox.io/s/54nyo0r78p If I click
To solve this now, I need to wrap App in an instance so this.props.data will contain the latest props, so I could reset. BTW having an instance I don't need to use Value as I already have state ;-) |
done in #156 |
Hey, thanks for this great library!
I find myself setting
initial=
values, updating them over time, and then performing an action at a certain point. And then, often after performing the action I need to "reset" the values to theirinitial
state again, to build towards another action.It would be nice if in addition
set(value)
, the components were passed areset()
function as well that always reset the value to theinitial
value.(Side note, why is
<State>
the only component that takes the function namedsetState
instead of simplyset
to stay consistent with the others?)The text was updated successfully, but these errors were encountered: