Recoil icon indicating copy to clipboard operation
Recoil copied to clipboard

[Request]: A way to resetAll in atomFamily

Open dliu120 opened this issue 4 years ago • 13 comments

Loving recoil so far!

It would be nice to have a way to call reset on all of the atoms in the atomFamily.

I have a form of dynamically generated inputs managed by the atomFamily state. I also have a reset button that should reset the entire form to default values. Instead of looping through my source of data to call reset on each atom, it would be nice to have an API to reset all of the atoms in a family.

dliu120 avatar Jun 23 '20 01:06 dliu120

@mondaychen Can I try this??

harshilparmar avatar Jul 14 '20 15:07 harshilparmar

@harshilparmar sure, we'd welcome PRs! But make sure you communicate the API here before you implement the whole thing.

@davidmccabe @drarmstr can one of you confirm whether this is a valid new feature to add to atomFamily?

mondaychen avatar Jul 14 '20 15:07 mondaychen

Sure, it would be reasonable to consider a shortcut mechanism for reseting all family state. We've seen that request from several users. Though, we would need to consider what the API looks like. It should also likely be marked as unstable, since we will be addressing memory management in the future, which may affect family semantics or how they are implemented.

drarmstr avatar Jul 14 '20 17:07 drarmstr

@drarmstr Sir can you help me to get started? I couldn't find any command for setup development.

harshilparmar avatar Aug 01 '20 17:08 harshilparmar

@harshilparmar we use yarn to manage dependencies. Install yarn and run yarn in the repo directory

mondaychen avatar Aug 03 '20 21:08 mondaychen

@mondaychen Ohh Thanks!! 👍

harshilparmar avatar Aug 04 '20 05:08 harshilparmar

@drarmstr May be also add reset to all atoms? This would be useful when logout without closing application.

artygrand avatar Sep 21 '20 05:09 artygrand

@drarmstr May be also add reset to all atoms? This would be useful when logout without closing application.

If you want a complete fresh slate of state, you may be able to consider adding a key to <RecoilRoot> or otherwise remounting it to use a fresh context.

drarmstr avatar Sep 23 '20 23:09 drarmstr

Would this API remove all the keys from a family? Or would it iterate through and reset all the atoms/selectors at each key?

justintoman avatar Nov 05 '20 16:11 justintoman

Would this API remove all the keys from a family? Or would it iterate through and reset all the atoms/selectors at each key?

That should just be an implementation detail that affects memory consumption / scalability. When an atom is reset it reverts to the default value. If no components or other nodes are subscribing to an atom then it may be eligible to be "garbage collected" and physically removed. But, we haven't finished working through the mechanism for cleanup and managing retention when necessary for un-subscribed atoms.

drarmstr avatar Nov 05 '20 21:11 drarmstr

Hello everybody, any news on this feature? My use case is also when the User logs out of the application, i would like to reset all the atoms to their default state. @drarmstr could you elaborate or provide a quick example of the workaround adding a key to <RecoilRoot>? I'm having trouble figuring that one out.

Thanks!

eduardobalbo avatar Jun 17 '21 20:06 eduardobalbo

@eduardobalbo You can just

Auth.logout().then(() => window.location.reload())

artygrand avatar Jun 18 '21 12:06 artygrand

For anyone unsure on the key resetting the RecoilRoot, heres an example on how you could update it using either Context or a Parent RecoilRoot:

  • https://codesandbox.io/s/recoil-js-reset-root-bvi74z?file=/src/App.js

philcon93 avatar Jul 13 '22 00:07 philcon93