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.

[Question] Stateful shared component

See original GitHub issue

I have a component that is reused across the application but it has a different state for each of its parents. Am i allowed to this? (currently working)

type Props = {
  branchState: RecoilState<string>;
};

export default function Branches({ branchState }: Props) {
  const [selectedBranch, selectBranch] = useRecoilState(branchState);
  ...
}

Would love your opinion.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
davidmccabecommented, Apr 14, 2021

If I understand correctly, the question is whether you can pass an atom in as a prop. If so, yes, this is a great way to do things.

0reactions
RayLuxembourgcommented, Apr 15, 2021

Something else I’ll do occasionally is make the prop optional and have the component define it’s own internal atom. That way users of the component don’t need to provide one if it’s isolated. But, if they want to sync state with something else, then they can provide an atom override.

Ye its all depend on the use case. If u are ok about sharing the state in different routes/view s then it’s great. For example in my case, branches has an async selector to get list of branches from server and has the current selected branch as an atom. The current selected is what i want to not be shared so each view has different state but the api call for branches is common so only 1 request is ever sent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to share states between components using ...
We move React hooks stateful logic from HookComponent to useShareableState . We call useShareableState using useBetween in each component.
Read more >
Stateless v/s Stateful Components in React - YouTube
react #StatefulvsStatelessHey Guys, welcome to my series where we will learn about commonly asked Interview Questions.
Read more >
How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >
Stateful and Stateless Components in React
Today, we're going to review what stateful and stateless components are in React, how you can tell the difference, and the complex process ......
Read more >
React Hooks Are Great to Reuse Stateful Logic But What's The ...
Summary. React Hooks is an excellent solution for sharing component stateful logic. However, when it comes to sharing stateful logic by Stateful Component...
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