UNPKG

kedao

Version:

Rich Text Editor Based On Draft.js

34 lines (33 loc) 1.37 kB
import { useRef, useEffect } from 'react'; // Hook function useWhyDidYouUpdate(_name, props) { // Get a mutable ref object where we can store props ... // ... for comparison next time this hook runs. const previousProps = useRef(); useEffect(() => { if (previousProps.current) { // Get all keys from previous and current props const allKeys = Object.keys(Object.assign(Object.assign({}, previousProps.current), props)); // Use this object to keep track of changed props const changesObj = {}; // Iterate through keys allKeys.forEach((key) => { // If previous is different from current if (previousProps.current[key] !== props[key]) { // Add to changesObj changesObj[key] = { from: previousProps.current[key], to: props[key] }; } }); // If changesObj not empty then output to console // if (Object.keys(changesObj).length) { // console.log('[why-did-you-update]', name, changesObj) // } } // Finally update previousProps with current props for next hook call previousProps.current = props; }); } export default useWhyDidYouUpdate;