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.

Accessing useSharedCounter within Components

See original GitHub issue

To 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:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cdriccommented, Jan 7, 2022

Amazing. Thank you so much for your help. Let me test this out and I will keep you posted if I have further question!

0reactions
donethingcommented, Apr 16, 2022

Hi @cdric,

You should not pass the useSharedCounter hook through the properties of the component. This is the advantage of “use-between”.

You should create a file “use-shared-counter.js” in which you will export the useSharedCounter hook. … Enjoy!

Hello,with React 18.0, I has a problem. index.tsx has changed to those, and the setCount is not working, count is always 0. What need I do to adapt 18.0? Thank you.

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import reportWebVitals from './reportWebVitals'

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
)

root.render(
  <React.StrictMode>
    <App/>
  </React.StrictMode>
)

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals()
Read more comments on GitHub >

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

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