@uiw/react-native
Version:
UIW for React Native
22 lines (21 loc) • 550 B
JavaScript
import { useRef, useEffect } from 'react';
/**
* 获取上一轮的 props 或 state
* How to get the previous props or state?
* https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state
* @example
* ```js
* function Counter() {
* const [count, setCount] = useState(0);
* const prevCount = usePrevious(count);
* return <h1>Now: {count}, before: {prevCount}</h1>;
* }
* ```
*/
export function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}