This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
Have scoped atoms with <RecoilScope> and useScopedRecoilState by leveraging react context, instead of atomFamily and arbitrary ids. #2250
lucasbordeau
started this conversation in
Ideas
Replies: 1 comment
-
Another feature that is required with this implementation would be to abstract away the need to have separated react Context for scoped atoms to reference specific contexts higher in the hierarchy, and not the direct parent context. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problems with atom family when you want to have locally scoped atoms :
I'll try to give some clues for a possible solution, it's not clear for me what would best fit for the multiple use cases of Recoil, but here's what would be interesting for us at Twenty.
Use case :
Let's take an example with a table, you want each row to have its internal scope, and components in each cell access the row's context.
We all agree here that we don't want to resort to useReducer and React contexts.
Although we can currently use a specific RowIDContext, as explained in the documentation, it feels like we're using some kind of non generic workaround.
What would be interesting to have is a generic RecoilScope provider component that would handle local scoping of our row.
RecoilScope provider
The RecoilScope component would abstract away the need to manage a React context for each scoped atom family.
useScopedRecoilState hook
Then any component in that hierarchy could call a scoped atom like this :
scopedAtoms
The API for the scoped atom is not really clear, it could be :
Clear policy / preventing memory leaks
We could imagine something like :
This would be a safe way to clean everything inside a Recoil scope, by using Recoil's internal we could access every atomFamily or scopedAtom that uses a particular id.
Conclusion
It currently works by hacking atomFamily a little bit and creating custom hooks but it could be really cleaner to provide it from Recoil directly.
We're currently implementing a solution based on atomFamily at Twenty, where we are happy to use Recoil next to Apollo, please check out our repository and how Recoil is used there.
Beta Was this translation helpful? Give feedback.
All reactions