useWhyDidYouUpdate
About
A hook that tracks which props or values changed to cause a component rerender. This is extremely useful for debugging performance issues and understanding why your component is re-rendering unnecessarily.
Examples
Basic example with component props
Advanced example with specific values
Example with conditional logging
Arguments
Argument | Type | Description | Default |
---|---|---|---|
componentName | string | Name of the component for identification in console logs | - |
currentProps | PropsRecord | Object containing the current props/values you want to track | - |
enableLogging | boolean | Whether to enable console logging (useful to disable in production) | true |
PropsRecord Type
A record object where keys are strings and values can be any type.
Returns
This hook doesn't return anything. It performs side effects by logging to the console when tracked values change.
Console Output
When props change, the hook logs information in the following format:
Usage Tips
-
Performance Debugging: Use this hook to identify which props are causing unnecessary rerenders in expensive components.
-
Object and Array Props: Pay special attention to object and array props that might be recreated on every render:
-
Production Usage: Consider disabling logging in production builds to avoid console pollution:
-
Selective Tracking: Instead of tracking all props, focus on the ones you suspect are causing issues: