mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
Adds docs for the new API
This commit is contained in:
parent
eb2a285aa7
commit
245e11681c
@ -1,6 +1,6 @@
|
||||
# `useGlobalState`
|
||||
|
||||
A React hook which creates a globally shared state.
|
||||
A React hook that creates a globally shared state.
|
||||
|
||||
## Usage
|
||||
|
||||
@ -30,3 +30,32 @@ const Demo: FC = () => {
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
It also allows initializing the state with a function and using a function in the setState:
|
||||
|
||||
```tsx
|
||||
const useGlobalValue = createGlobalState<number>(() => 0);
|
||||
|
||||
const CompA: FC = () => {
|
||||
const [value, setValue] = useGlobalValue();
|
||||
|
||||
return <button onClick={() => setValue(value => value + 1)}>+</button>;
|
||||
};
|
||||
|
||||
const CompB: FC = () => {
|
||||
const [value, setValue] = useGlobalValue();
|
||||
|
||||
return <button onClick={() => setValue(value => value - 1)}>-</button>;
|
||||
};
|
||||
|
||||
const Demo: FC = () => {
|
||||
const [value] = useGlobalValue();
|
||||
return (
|
||||
<div>
|
||||
<p>{value}</p>
|
||||
<CompA />
|
||||
<CompB />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user