question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use it in a non singleton way, ie: as a state for a reusable component ?

See original GitHub issue

Great 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Sep 21, 2020

@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.


I wanna hold its state in atoms rather than react state to avoid unnecessary re-renders.

OK, now I see your point.

const Input = () => {
const validAtom = useRef(atom(false)).current
const charCountAtom = useRef(atom(0)).current

function handleChange() {
	// ... handle change 
}

return <div>
    <InputIcon validAtom={validAtom} charCountAtom={carCountAtom} />
    <InputLabel validAtom={validAtom} charCountAtom={carCountAtom} />
    <BaseInput validAtom={validAtom} charCountAtom={AtomcarCountAtom} onChange={handleChange}/>
    <inputCountIndicator validAtom={validAtom} charCountAtom={carCountAtom} />
    ... etc.
  </div>
}

Something like this?

There can be various patterns.

Explicit lazy ref initialization:

const validAtom = useRef()
if (!validAtom.current) {
  validAtom.current = atom(false)
}

Create multiple atoms in a ref:

const atoms = useRef()
if (!atoms.current) {
  atoms.current = {
    valid: atom(false),
    charCount: atom(0),
  }
}

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.

1reaction
dai-shicommented, Oct 4, 2020

@a-eid Do you have any other questions? If we have concrete ideas, we could write something in docs.

#5 is a related issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found