import { SetStateAction, useState, Dispatch } from 'react' type Props = { state?: T setState?: Dispatch> } type Returns = [T, Dispatch> | ((state: T) => void)] /** * React hooks that lets you pass an optional state and setState function. This is useful when you sometimes control state or do it internally, but don't know which beforehand. * * The internal state is a fallback if you don't provide a state and setState argument. * * @param props array with the controlled state and setState function * @param initialValue optional initial value for the internal state */ export const useControlledOrInternalState = ( props: Props, initialValue: T ): Returns => { let [state, setState] = useState(initialValue) // let finalState = state // let setFinalState = setState if (props.state !== undefined) { state = props.state if (props.setState) { setState = props.setState } } return [state, setState] }