mirror of
https://github.com/muerwre/muerwre.github.io.git
synced 2025-04-25 10:56:40 +07:00
30 lines
No EOL
791 B
Markdown
30 lines
No EOL
791 B
Markdown
```typescript
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
/** Pass dictionary of `props` as argument and it will
|
|
* tell you, which one changed after rerender.
|
|
* Use `prefix` to distinguish props of different components.
|
|
*/
|
|
// eslint-disable-next-line import/no-unused-modules
|
|
export const useWhatsChanged = (
|
|
props: Record<string, unknown>,
|
|
prefix = '',
|
|
) => {
|
|
const prevProps = useRef(props);
|
|
|
|
useEffect(() => {
|
|
Object.entries(props).forEach(([key, value]) => {
|
|
if (
|
|
!Object.prototype.hasOwnProperty.call(prevProps.current, key) ||
|
|
prevProps.current[key] !== value
|
|
) {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`${prefix} ${key} has changed`);
|
|
}
|
|
});
|
|
|
|
prevProps.current = props;
|
|
}, [props, prefix]);
|
|
};
|
|
|
|
``` |