-
Notifications
You must be signed in to change notification settings - Fork 0
/
what-props-changed.js
73 lines (61 loc) · 1.87 KB
/
what-props-changed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useEffect, useRef } from 'react';
/*
This util Hook is useful for determing why a function component is re-rendering.
It should never be left in production code; only use this for debugging purposes.
USAGE:
function MyComponent(props) {
useWhatPropsChanged(props);
return <div>{props.children}</div>;
}
From this Stack Overflow answer:
https://stackoverflow.com/a/51082563/5253702
*/
function useWhatPropsChanged(props) {
const prev = useRef(props);
useEffect(() => {
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
if (prev.current[k] !== v) {
ps[k] = [prev.current[k], v];
}
return ps;
}, {});
if (Object.keys(changedProps).length > 0) {
console.log('Changed props:', changedProps);
}
prev.current = props;
});
}
/*
This util is useful for determing why a class component is re-rendering.
It should never be left in production code; only use this for debugging purposes.
USAGE:
class MyComponent extends Component {
componentDidUpdate(prevProps, prevState) {
whatPropsChanged(this.props, prevProps, this.state, prevState);
}
}
From this Stack Overflow answer:
https://stackoverflow.com/a/51082563/5253702
*/
function whatPropsChanged(props, prevProps, state, prevState) {
if (props && prevProps) {
Object.entries(props).forEach(([key, val]) => {
const oldVal = prevProps[key];
if (oldVal !== val) {
console.log(`Prop '${key}' changed.`);
console.log(' Old val: ', oldVal);
console.log(' New val: ', val);
}
});
}
if (state && prevState) {
Object.entries(state).forEach(([key, val]) => {
const oldVal = prevState[key];
if (oldVal !== val) {
console.log(`State '${key}' changed`);
console.log(' Old val: ', oldVal);
console.log(' New val: ', val);
}
});
}
}