npm.io
2.0.5 • Published 4 years ago

@alevnyacow/shared-react-variables

Licence
MIT
Version
2.0.5
Deps
2
Size
15 kB
Vulns
1
Weekly
0
Stars
2

Shared React variables

npm GitHub GitHub last commit npm GitHub top language code style: prettier

About

Shared React variables provide you easy and comfortable global state management based on hooks. You can treat it like a global mutable useState. Isn't it awesome?

Example

import { createUseSharedVariable } from "@alevnyacow/shared-react-variables"; 

const [useTimer] = createUseSharedVariable({ ticks: 0 });

const Timer = () => {
    const timer = useTimer();
    useEffect(() => {
        setInterval(() => {
            timer.ticks++;
        }, 1000);
    }, []);

    return <div>{timer.ticks}</div>
};

const AnotherTimerWithSameState = () => {
    // using pretty same timer in another place
    const timer = useTimer();

    return <div>{timer.ticks}</div>
};

Codesandbox

https://codesandbox.io/s/react-shared-variables-example-f7feo7

Keywords