Search code examples
recoiljs

Does it matter where you declare Atoms when using Recoil?


I was going over the the Getting Started section of the docs, but it wasn't clear from the examples in which files the Atoms were being declared.

Does it matter where you declare a particular Atom in the app? If two components subscribe to the same Atom, can I declare the atom in one component's file or the other? Or do I need to declare it in both? Or do I need to declare it in a third file? Is there a best practice?

My intuition is telling me that I should declare these Atoms in a third file and import them to the components that need them, but I just want to be sure I'm understanding this correctly.


Solution

  • It's generally the right approach to declare and export the recoil states (atoms/selectors/etc.) in one module, and then import them into the other component modules which use them. If you have a data structure that requires a different approach, it will be apparent to you.

    You can research GitHub repositories in these topics to see examples and improve your confidence: