-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3320 from marmelab/pref-optimization-3-0
[RFR] List performance optimizations
- Loading branch information
Showing
10 changed files
with
185 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useRef, useEffect } from 'react'; | ||
|
||
/** | ||
* Debug hook showing which props updated between two renders | ||
* @example | ||
* | ||
* const MyComponent = React.memo(props => { | ||
* useWhyDidYouUpdate('MyComponent', props); | ||
* return <div...; | ||
* }); | ||
* | ||
* @link https://usehooks.com/useWhyDidYouUpdate/ | ||
*/ | ||
export default function useWhyDidYouUpdate(name, props) { | ||
// Get a mutable ref object where we can store props ... | ||
// ... for comparison next time this hook runs. | ||
const previousProps = useRef() as any; | ||
|
||
useEffect(() => { | ||
if (previousProps.current) { | ||
// Get all keys from previous and current props | ||
const allKeys = Object.keys({ ...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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.