Skip to content

Commit c0ace17

Browse files
committed
feat: 🎸 Added useDidChange hook
1 parent 659def8 commit c0ace17

6 files changed

+59
-19
lines changed

README.md

+25-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
<img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
1818
</a>
1919
<a href="https://www.npmjs.com/package/@better-hooks/lifecycle">
20-
<img src="https://custom-icon-badges.demolab.com/bundlephobia/min/@better-hooks/lifecycle?color=64BC4B&logo=package" />
20+
<img src="https://custom-icon-badges.demolab.com/npm/v/@better-hooks/lifecycle.svg?logo=npm&color=E10098" />
2121
</a>
2222
<a href="https://www.npmjs.com/package/@better-hooks/lifecycle">
23-
<img src="https://custom-icon-badges.demolab.com/npm/v/@better-hooks/lifecycle.svg?logo=npm&color=E10098" />
23+
<img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/@better-hooks/lifecycle?color=blueviolet&logo=package" />
24+
</a>
25+
<a href="https://www.npmjs.com/package/@better-hooks/lifecycle">
26+
<img src="https://custom-icon-badges.demolab.com/npm/dm/@better-hooks/lifecycle?logoColor=fff&logo=trending-up" />
2427
</a>
2528
</p>
2629

@@ -36,6 +39,8 @@ React lifecycle turned into dev friendly and readable hooks
3639

3740
💎 **No external dependencies**
3841

42+
🪄 **Increases code readability**
43+
3944
## Installation
4045

4146
```bash
@@ -54,9 +59,17 @@ yarn add @better-hooks/lifecycle
5459

5560
```tsx
5661
import React from "react";
57-
import { useDidMount, useDidRender, useDidUpdate, useWillUnmount,useIsMounted, useWillMount } from "@better-hooks/lifecycle";
58-
59-
const MyComponent: React.FC = () => {
62+
import {
63+
useDidMount,
64+
useDidUpdate,
65+
useWillUnmount,
66+
useIsMounted,
67+
useWillMount,
68+
useForceUpdate,
69+
useDidChange
70+
} from "@better-hooks/lifecycle";
71+
72+
const MyComponent: React.FC = (props) => {
6073
const [isOpen, setIsOpen] = React.useState(false)
6174

6275
// returns ref with the mounted boolean state
@@ -75,11 +88,6 @@ const MyComponent: React.FC = () => {
7588
// ...
7689
})
7790

78-
// Called second, when initial DOM changes are triggered
79-
useDidRender(() => {
80-
// ...
81-
})
82-
8391
// Called when isOpen change
8492
useDidUpdate(() => {
8593
// ...
@@ -90,6 +98,13 @@ const MyComponent: React.FC = () => {
9098
// ...
9199
}, [isOpen], true)
92100

101+
// Called when dependencies change, we can inspect previous dependencies
102+
useDidChange((prevProps) => {
103+
if(prevProps.value !== props.value) {
104+
// ...
105+
}
106+
}, [props], true)
107+
93108
// Called last
94109
useWillUnmount(() => {
95110
// ...

src/hooks/use-did-change.hook.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useRef } from "react";
2+
3+
export const useDidChange = <T extends any[]>(
4+
callback: VoidFunction | ((previousDependencies: T | null) => VoidFunction),
5+
dependencies: T,
6+
useOnMount = false,
7+
) => {
8+
const prev = useRef<T | null>(null);
9+
const mountRef = useRef(useOnMount);
10+
11+
useEffect(() => {
12+
if (!mountRef.current) {
13+
mountRef.current = true;
14+
return;
15+
}
16+
17+
return () => {
18+
callback(prev.current);
19+
prev.current = dependencies;
20+
};
21+
// eslint-disable-next-line react-hooks/exhaustive-deps
22+
}, dependencies);
23+
};

src/hooks/use-did-mount.hook.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import { useEffect } from "react";
23

3-
export const useDidMount = (callback: VoidFunction | (() => VoidFunction)) =>
4+
export const useDidMount = (callback: VoidFunction | (() => VoidFunction)) => {
45
useEffect(callback, []);
6+
};

src/hooks/use-did-render.hook.ts

-4
This file was deleted.

src/hooks/use-did-update.hook.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import { useEffect, useRef } from "react";
23

34
export const useDidUpdate = (
45
callback: VoidFunction | (() => VoidFunction),
56
dependencies: any[],
67
useOnMount = false,
78
) => {
8-
const didMountRef = useRef(useOnMount);
9+
const mountRef = useRef(useOnMount);
910

1011
useEffect(() => {
11-
if (!didMountRef.current) {
12-
didMountRef.current = true;
12+
if (!mountRef.current) {
13+
mountRef.current = true;
1314
return;
1415
}
1516

src/hooks/use-will-unmount.hook.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import { useEffect } from "react";
23

3-
export const useWillUnmount = (callback: VoidFunction) => useEffect(() => callback, []);
4+
export const useWillUnmount = (callback: VoidFunction) => {
5+
useEffect(() => callback, []);
6+
};

0 commit comments

Comments
 (0)