Skip to content

Accessing useSharedCounter within Components #22

@cdric

Description

@cdric

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;

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions