-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(usecontrolledstate): refactor usecontrolledstate (#1369)
- Loading branch information
zhaoting zhou
authored
Oct 20, 2021
1 parent
ba32640
commit 06750d0
Showing
4 changed files
with
29 additions
and
45 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import { useState, useEffect, useRef } from 'react'; | ||
import { useState, useEffect, useRef, Dispatch, SetStateAction, useDebugValue } from 'react'; | ||
import { isUndefined } from 'lodash'; | ||
|
||
function format<T>([innerState, outterState]: [T, T]) { | ||
const isControlled = isUndefined(outterState) ? 'uncontrolled' : 'controlled'; | ||
const value = isUndefined(outterState) ? innerState : outterState; | ||
return `State is ${isControlled} | Value is ${value}`; | ||
} | ||
|
||
const useControlledState = <T>( | ||
controlledState: T | (() => T) | undefined, | ||
empty: T | ||
): [T, (_state: T | (() => T), force?: boolean) => void] => { | ||
const [_controlledState, setControlledState] = useState<T>(controlledState === undefined ? empty : controlledState); | ||
|
||
const setState = useRef((_state: T | (() => T), force = false) => { | ||
if (controlledState === undefined || force) { | ||
setControlledState(_state); | ||
outterState?: T | (() => T), | ||
defaultOutterState?: T, | ||
callback?: (state: T | (() => T)) => void | ||
): [T | undefined, (state: T | Dispatch<SetStateAction<T>>, force?: boolean) => void] => { | ||
const [innerState, setInnerState] = useState<T>(isUndefined(outterState) ? defaultOutterState : outterState); | ||
|
||
const setState = useRef((state: T | (() => T), force = false) => { | ||
if (isUndefined(outterState) || force) { | ||
setInnerState(state); | ||
} | ||
|
||
callback?.(state); | ||
}).current; | ||
|
||
useDebugValue([innerState, outterState], format); | ||
|
||
useEffect(() => { | ||
if (controlledState !== undefined) { | ||
setControlledState(controlledState); | ||
if (!isUndefined(outterState)) { | ||
setInnerState(outterState); | ||
} | ||
}, [controlledState]); | ||
}, [outterState]); | ||
|
||
return [_controlledState, setState]; | ||
return [innerState, setState]; | ||
}; | ||
|
||
export default useControlledState; |
06750d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: