-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify syncing props to state in
ThemeProvider
(#4855)
* simplify syncing updates for theme provider * simplify syncing updates for theme provider
- Loading branch information
1 parent
f96f609
commit 873249a
Showing
4 changed files
with
101 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': patch | ||
--- | ||
|
||
avoid useeffect when syncing theme config |
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
63 changes: 63 additions & 0 deletions
63
packages/react/src/hooks/__tests__/useSyncedState.test.tsx
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,63 @@ | ||
import {act, renderHook} from '@testing-library/react' | ||
import {useSyncedState} from '../useSyncedState' | ||
|
||
const renderUseSyncedState = ( | ||
initialValue: string | (() => string), | ||
{isPropUpdateDisabled}: {isPropUpdateDisabled: boolean} = {isPropUpdateDisabled: false}, | ||
) => { | ||
return renderHook(props => useSyncedState(props.initialValue, {isPropUpdateDisabled: props.isPropUpdateDisabled}), { | ||
initialProps: { | ||
initialValue, | ||
isPropUpdateDisabled, | ||
}, | ||
}) | ||
} | ||
test('it renders a default', () => { | ||
const {result} = renderUseSyncedState('default') | ||
expect(result.current[0]).toEqual('default') | ||
}) | ||
|
||
test('it updates state from the internal state setter', () => { | ||
const {result} = renderUseSyncedState('default') | ||
expect(result.current[0]).toEqual('default') | ||
act(() => { | ||
result.current[1]('new value') | ||
}) | ||
expect(result.current[0]).toEqual('new value') | ||
}) | ||
|
||
test('it updates state from the internal state setter with an updater fn', () => { | ||
const {result} = renderUseSyncedState('default') | ||
expect(result.current[0]).toEqual('default') | ||
act(() => { | ||
result.current[1](prev => `${prev} new value`) | ||
}) | ||
expect(result.current[0]).toEqual('default new value') | ||
}) | ||
|
||
test('it updates state from the external state setter', () => { | ||
const {result, rerender} = renderUseSyncedState('default') | ||
expect(result.current[0]).toEqual('default') | ||
|
||
rerender({initialValue: 'new value', isPropUpdateDisabled: false}) | ||
|
||
expect(result.current[0]).toEqual('new value') | ||
}) | ||
|
||
test('it properly handles init functions', () => { | ||
const {result, rerender} = renderUseSyncedState(() => 'default') | ||
expect(result.current[0]).toEqual('default') | ||
|
||
rerender({initialValue: () => 'new value', isPropUpdateDisabled: false}) | ||
|
||
expect(result.current[0]).toEqual('new value') | ||
}) | ||
|
||
test('it does not update from props if disabled', () => { | ||
const {result, rerender} = renderUseSyncedState('default', {isPropUpdateDisabled: true}) | ||
expect(result.current[0]).toEqual('default') | ||
|
||
rerender({initialValue: 'new value', isPropUpdateDisabled: true}) | ||
|
||
expect(result.current[0]).toEqual('default') | ||
}) |
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,28 @@ | ||
import {useState} from 'react' | ||
|
||
/** | ||
* When the value that initialized the state changes | ||
* this hook will update the state to the new value, immediately. | ||
* | ||
* This uses an Object.is comparison to determine if the value has changed by default | ||
* | ||
* If you use a non-primitive value as the initial value, you should provide a custom isEqual function | ||
* | ||
* This is adapted almost directly from https://beta.reactjs.org/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes | ||
*/ | ||
|
||
export const useSyncedState = <T,>( | ||
initialValue: T | (() => T), | ||
{isPropUpdateDisabled = false, isEqual = Object.is} = {}, | ||
) => { | ||
const [state, setState] = useState(initialValue) | ||
const [previous, setPrevious] = useState(initialValue) | ||
|
||
const nextInitialValue = initialValue instanceof Function ? initialValue() : initialValue | ||
if (!isPropUpdateDisabled && !isEqual(previous, nextInitialValue)) { | ||
setPrevious(nextInitialValue) | ||
setState(nextInitialValue) | ||
} | ||
|
||
return [state, setState] as const | ||
} |