mirror of
https://github.com/muerwre/muerwre.github.io.git
synced 2025-04-24 18:36:41 +07:00
add useWhatsChanged
This commit is contained in:
parent
42098fc50a
commit
cdb1309a81
2 changed files with 33 additions and 0 deletions
30
content/Frontend/React/useWhatsChanged.md
Normal file
30
content/Frontend/React/useWhatsChanged.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
```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]);
|
||||
};
|
||||
|
||||
```
|
3
content/Linux/Find out who uses swap.md
Normal file
3
content/Linux/Find out who uses swap.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
```shell
|
||||
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue