Skip to main content

useStateListeningProp

examples

查看控制台输出


  • test10577

source code

import { useState, useRef, useEffect, Dispatch, SetStateAction } from 'react';

type StateListeningPropResult<T> = [T, Dispatch<SetStateAction<T>>];

const useStateListeningProp = <T>(prop: T): StateListeningPropResult<T> => {
const [state, setState] = useState<T>(prop);
const previousPropRef = useRef<T>();

if (prop !== previousPropRef.current && prop !== state) {
setState(prop);
}

useEffect(() => {
previousPropRef.current = prop;
}, [prop]);

return [state, setState];
};

export default useStateListeningProp;