Accessing useSharedCounter within Components
See original GitHub issueTo make my code cleaner I’m exporting my Component into their separate file. By doing so I’m having an issue as I can’t access useSharedCounter
Should I try to pass Rather than
const useSharedCounter = () => useBetween(useCounter);
const Count = () => {
const { count } = useSharedCounter();
return <p>{count}</p>;
};
const Buttons = () => {
const { inc, dec } = useSharedCounter();
return (
<>
<button onClick={inc}>+</button>
<button onClick={dec}>-</button>
</>
);
};
const App = () => (
<>
<Count />
<Buttons />
<Count />
<Buttons />
</>
);
export default App;
I am trying to do the following But I can’t see to retrieve the useSharedCounter={useSharedCounter} function in either Button.js or Count.js Is this possibel to perform such refactoring with this library?
import Button from "./Button.js'
import Count from "./Count.js'
const useSharedCounter = () => useBetween(useCounter);
const App = () => (
<>
<Count useSharedCounter={useSharedCounter} />
<Buttons useSharedCounter={useSharedCounter} />
<Count useSharedCounter={useSharedCounter} />
<Buttons useSharedCounter={useSharedCounter} />
</>
);
export default App;
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Sharing state between React components
So we can call one hook in different components and work together on one state. When updating the shared state, each component using...
Read more >How can I read a variable from another component in React?
I receive these two values from two options elements in a component using React. Question: How can I read these two variables from...
Read more >Sharing State Between React Components - Morioh
When you want to separate your React hooks between several components it's can be very difficult, because all context data stored in React...
Read more >Sharing State data between Function Components in React
In this video we will learn1. What is a Parent Component, What is a Child Component2. How to Share State data between two...
Read more >Full React Tutorial #5 - Multiple Components - YouTube
Hey gang, in this React tutorial we'll see how to add additional components to our React application (a navbar component and a homepage ......
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
Amazing. Thank you so much for your help. Let me test this out and I will keep you posted if I have further question!
Hello,with React 18.0, I has a problem.
index.tsx
has changed to those, and thesetCount
is not working,count
is always 0. What need I do to adapt 18.0? Thank you.