How to use it in a non singleton way, ie: as a state for a reusable component ?
See original GitHub issueGreat work thank you!!
I was thinking how could I use atoms in a non singleton way, for example if I’m making a reusable Component Dropdown
& I wanna hold onto some state for each instance of the Component in atoms like for a dropdown it would be options
, how do I do that ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
State management with React Hooks and Reusable - Medium
Enter 'Reusable' One of the benefits of Reusable is that there is no initialization code. If you want to develop an open source...
Read more >The Singleton Pattern - Topcoder
The singleton pattern is the most over-used pattern of all the patterns (almost 10% of the components implement this pattern) and is probably ......
Read more >Dagger basics - Android Developers
Inside the @Component interface, you can define functions that return instances of the classes you need (i.e. UserRepository ). @Component tells ...
Read more >Simplest/cleanest way to implement a singleton in JavaScript
If you are using ES6, you can represent a singleton using ES Modules very easily, and you can even hold private state by...
Read more >ASP.NET Core Blazor state management - Microsoft Learn
Child components can work with persisted data without regard to the state persistence mechanism. In the following example of a ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@a-eid Can you create a codesandbox example to show what you would like to do? Even if it’s incomplete. Then, we could help based on it. What you described sounds like something possible with useState.
OK, now I see your point.
Something like this?
There can be various patterns.
Explicit lazy ref initialization:
Create multiple atoms in a ref:
Instead of useRef, we could use useState, useMemoOne, or even useAtom.
We could also use atomFamily. It’s a variant of singleton, but it would work for this use case.
It would be still worth for a codesandbox example, so that we can discuss more concretely, and have better ideas.
@a-eid Do you have any other questions? If we have concrete ideas, we could write something in docs.
#5 is a related issue.