Created
January 21, 2021 02:48
-
-
Save mgtitimoli/cc2a20aa2d9748b5d59b773b68b359dd to your computer and use it in GitHub Desktop.
StateAccessor (getter, setter) Hook => useStateAccessor
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
import React from "react"; | |
import type {ReactStateAccessor} from "./useStateAccessor"; | |
type State = { | |
count: number; | |
}; | |
type StateAccessor = ReactStateAccessor<State> | |
const increaseCount = ({count}: State) => ({count: count + 1}); | |
const render = (stateAccesor: StateAccessor) => ( | |
<div> | |
<div>Count: {stateAccessor()}</div> | |
<button onClick={() => stateAccessor()}>Count: {stateAccessor(increaseCount)}</button> | |
<div> | |
) | |
const initialState = { | |
count: 0 | |
}; | |
const Test = () => | |
render(useStateAccessor(initialState)); | |
export default Test; |
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
import {useRef, useState} from "react"; | |
import type {SetStateAction} from "react"; | |
type ReactStateAccessor<State> = ( | |
...args: [] | [SetStateAction<State>] | |
) => State; | |
const useStateAccessors = <State>( | |
initialState: State | |
): ReactStateAccessor<State> => { | |
const [, setState] = useState(initialState); | |
const stateRef = useRef(initialState); | |
return (...args) => { | |
if (args.length === 1) { | |
const [newStateOrSetter] = args; | |
stateRef.current = | |
newStateOrSetter instanceof Function | |
? newStateOrSetter(stateRef.current) | |
: newStateOrSetter; | |
setState(stateRef.current); | |
} | |
return stateRef.current; | |
}; | |
}; | |
export default useStateAccessors; | |
export type {ReactStateAccessor}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment