-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add createGlobalState hook generator
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# `useGlobalState` | ||
|
||
A React hook which creates a globally shared state. | ||
|
||
## Usage | ||
|
||
```tsx | ||
const useGlobalValue = createGlobalState<number>(0); | ||
|
||
const CompA: FC = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <button onClick={() => setValue(value + 1)}>+</button>; | ||
}; | ||
|
||
const CompB: FC = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <button onClick={() => setValue(value - 1)}>-</button>; | ||
}; | ||
|
||
const Demo: FC = () => { | ||
const [value] = useGlobalValue(); | ||
return ( | ||
<div> | ||
<p>{value}</p> | ||
<CompA /> | ||
<CompB /> | ||
</div> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useLayoutEffect, useState } from 'react'; | ||
import useEffectOnce from './useEffectOnce'; | ||
|
||
export function createGlobalState<S = any>(initialState?: S) { | ||
const store: { state: S | undefined; setState: (state: S) => void; setters: any[] } = { | ||
state: initialState, | ||
setState(state: S) { | ||
store.state = state; | ||
store.setters.forEach(setter => setter(store.state)); | ||
}, | ||
setters: [], | ||
}; | ||
|
||
return (): [S | undefined, (state: S) => void] => { | ||
const [globalState, stateSetter] = useState<S | undefined>(store.state); | ||
|
||
useEffectOnce(() => () => { | ||
store.setters = store.setters.filter(setter => setter !== stateSetter); | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
if (!store.setters.includes(stateSetter)) { | ||
store.setters.push(stateSetter); | ||
} | ||
}); | ||
|
||
return [globalState, store.setState]; | ||
}; | ||
} | ||
|
||
export default createGlobalState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { storiesOf } from "@storybook/react"; | ||
import React, { FC } from "react"; | ||
import { createGlobalState } from "../src"; | ||
import ShowDocs from "./util/ShowDocs"; | ||
|
||
const useGlobalValue = createGlobalState<number>(0); | ||
|
||
const CompA: FC = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <button onClick={() => setValue(value + 1)}>+</button>; | ||
}; | ||
|
||
const CompB: FC = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <button onClick={() => setValue(value - 1)}>-</button>; | ||
}; | ||
|
||
const Demo: FC = () => { | ||
const [value] = useGlobalValue(); | ||
return ( | ||
<div> | ||
<p>{value}</p> | ||
<CompA /> | ||
<CompB /> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf("State|createGlobalState", module) | ||
.add("Docs", () => <ShowDocs md={require("../docs/createGlobalState.md")} />) | ||
.add("Demo", () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import createGlobalState from '../src/createGlobalState'; | ||
|
||
describe('useGlobalState', () => { | ||
it('should be defined', () => { | ||
expect(createGlobalState).toBeDefined(); | ||
}); | ||
|
||
it('both components should be updated', () => { | ||
const useGlobalValue = createGlobalState(0); | ||
const { result: result1 } = renderHook(() => useGlobalValue()); | ||
const { result: result2 } = renderHook(() => useGlobalValue()); | ||
expect(result1.current[0] === 0); | ||
expect(result2.current[0] === 0); | ||
act(() => { | ||
result1.current[1](1); | ||
}); | ||
expect(result1.current[0] === 1); | ||
expect(result2.current[0] === 1); | ||
}); | ||
}); |