UNPKG

@coreui/react-pro

Version:

UI Components Library for React.js

15 lines (13 loc) 401 B
import { Dispatch, SetStateAction, useEffect, useState } from 'react' export const useStateWithCallback = <S>( initialState: S, handler?: (prevState: S) => void, runHandler?: boolean, ): [S, Dispatch<SetStateAction<S>>] => { const [state, setState] = useState<S>(initialState) handler && useEffect(() => { runHandler && handler(state) }, [state]) return [state, setState] }