Giter Site home page Giter Site logo

Comments (5)

cdric avatar cdric commented on June 2, 2024 1

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

from use-between.

cdric avatar cdric commented on June 2, 2024

Any update on this? I would truly appreciate.
At this point, unless I can fix this I'm gonna probably gonna use a different library...

from use-between.

betula avatar betula commented on June 2, 2024

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.

// ./use-shared-counter.js

import { useCallback, useState } from "react";
import { useBetween } from "use-between";

const useCounter = () => {
  const [count, setCount] = useState(0);
  const inc = useCallback(() => setCount((c) => c + 1), []);
  const dec = useCallback(() => setCount((c) => c - 1), []);
  return {
    count,
    inc,
    dec
  };
};

export const useSharedCounter = () => useBetween(useCounter);

Next, you just use the useSharedCounter hook in any component.

// ./Buttons.js

import { useSharedCounter } from "./use-shared-counter";

export const Buttons = () => {
  const { inc, dec } = useSharedCounter();
  return (
    <>
      <button onClick={inc}>+</button>
      <button onClick={dec}>-</button>
    </>
  );
};
// ./Count.js

import { useSharedCounter } from "./use-shared-counter";

export const Count = () => {
  const { count } = useSharedCounter();
  return <p>{count}</p>;
};

Edit on codesandbox

Enjoy!

from use-between.

pe-johndpope avatar pe-johndpope commented on June 2, 2024

in my code base that I inherited - there's a helper for api.

a bloated example -
but gist is it's in a separate file - not in a component -
I guess question is beyond these api helpers be put into a component?
or continue to pass shared setters / getters via paramaeters? or is there a better way?
I have a DataManager class that's a singleton - it's allowed me to get data for android / ios before -
the whole redux thing is a quagmire and this mostly solves this headache apart from this edge case.

export async function reLoginUser(setShowLogin:any,setAuthenticated:any) {

    const credentials =  { Username: email, Password: pwd };

    NocApi.post('/logon',credentials)
        .then(response => {
            console.log("response:",response);
     

                if (token) {
                    Promise.all([
                        setUser(credentials),
                    ])
                        .then(async (res) => {
                            console.log('navigation go HomeScreen');
                            setShowLogin(false); // HERE 
                            setAuthenticated(true);
                        })
                }
            }
        })
}

In above example I'm passing state variables around to external function -
I get it's not needed when used within a component - but here can't see any other way.
It's possible I could introduce a DataManager singleton -but maybe an overkilll

UPDATE:
exploring this
Screen Shot 2022-03-26 at 5 56 28 am

Screen Shot 2022-03-26 at 6 08 35 am

from use-between.

donething avatar donething commented on June 2, 2024

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()

from use-between.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.