@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
22 lines (21 loc) • 518 B
JavaScript
import { useEffect, useRef } from 'react';
/**
* Get previous props value with React Hooks
* @param value
*
* Usage:
* import {usePreviousValue} from '@selfcommunity/react-core';
*
* const MyComponent = ({ count }) => {
* const prevCount = usePreviousValue(count);
* return (<div> {count} | {prevCount}</div>);
* }
*/
const usePreviousValue = (value) => {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
export default usePreviousValue;