Skip to content

Commit bac84f1

Browse files
docs
1 parent 485b731 commit bac84f1

23 files changed

+30505
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
docs/node_modules
3+
docs/.docusaurus
4+
docs/build
5+
docs/src/hooks
6+
dist
7+
example
8+
test

docs/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3+
};

docs/docs/Installation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
id: hooks
3+
---
4+
5+
# Getting started
6+
7+
Introduction to all utils and things
8+
9+
10+
## NPM detils
11+
12+
## Usage
13+
14+

docs/docs/useDebounce.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# useDebounce
2+
3+
A hook for debouncing.
4+
5+
## Examples
6+
7+
```jsx live
8+
function DebounceExample(props) {
9+
const delayInMilliseconds = 1000;
10+
const [query, setQuery] = React.useState('');
11+
const debouncedQuery = useDebounce(query, delayInMilliseconds);
12+
13+
return (
14+
<>
15+
Debounced value will get updated after {delayInMilliseconds}ms once you
16+
stop typing
17+
<br />
18+
<input
19+
value={query}
20+
onChange={e => setQuery(e.target.value)}
21+
placeholder="Enter query"
22+
/>
23+
<br />
24+
Entered value: <b>{query}</b>
25+
<br />
26+
Debounced value: <b>{debouncedQuery}</b>
27+
<br />
28+
</>
29+
);
30+
}
31+
```
32+
33+
## API
34+
35+
```typescript
36+
const debouncedValue = useDebounce(
37+
value: any,
38+
delay?: number
39+
);
40+
```
41+
42+
### Params
43+
44+
| Property | Description | Type | Default |
45+
| -------- | -------------------------- | -------- | ------- |
46+
| value | The value to be debounced. | `any` | - |
47+
| delay | Delay in milliseconds | `number` | 500 |

docs/docs/useIsMounted.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# useIsMounted
2+
3+
This hook is used to check whether the component is mounted or not. This hook will return a function which return a boolean value stating the component is mounted or not on invoking. This will be useful if you want to perform some operation based on component is mounted or not like stop polling an api, update state etc.
4+
5+
## Usage
6+
7+
```typescript
8+
const isMounted = useIsMounted();
9+
if (isMounted()) {
10+
// continue
11+
}
12+
```
13+
14+
## Examples
15+
16+
## API
17+
18+
import Tabs from '@theme/Tabs';
19+
import TabItem from '@theme/TabItem';
20+
21+
<Tabs>
22+
<TabItem value="js" label="JavaScript">
23+
24+
```js
25+
const isMounted = useIsMounted();
26+
```
27+
28+
</TabItem>
29+
<TabItem value="ts" label="Typescript">
30+
31+
```typescript
32+
const isMounted: () => Boolean = useIsMounted();
33+
```
34+
35+
</TabItem>
36+
37+
</Tabs>

docs/docs/usePrevious.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# usePrevious
2+
3+
If you want to access the previous props or state in functional components, you can use the `usePrevious` hook. This hook would work for props, state, or any other calculated value.
4+
5+
## Usage
6+
7+
```typescript
8+
const [value, setvalue] = useState(initialState);
9+
const previousValue = usePrevious(value);
10+
```
11+
12+
## Examples
13+
14+
```jsx live
15+
function PreviousStateExample(props) {
16+
const [count, setCount] = useState(0);
17+
const prevCount = usePrevious(count);
18+
19+
return (
20+
<div>
21+
Current value: <b>{count}</b>, Previous value: <b>{prevCount}</b>
22+
<br />
23+
<button onClick={() => setCount(count + 1)}>+ count</button>
24+
<button
25+
style={{ marginLeft: '10px' }}
26+
onClick={() => setCount(count - 1)}
27+
>
28+
- count
29+
</button>
30+
</div>
31+
);
32+
}
33+
```
34+
35+
## API
36+
37+
import Tabs from '@theme/Tabs';
38+
import TabItem from '@theme/TabItem';
39+
40+
<Tabs>
41+
<TabItem value="js" label="JavaScript">
42+
43+
```js
44+
const previousState = usePrevious(state);
45+
```
46+
47+
</TabItem>
48+
<TabItem value="ts" label="Typescript">
49+
50+
```typescript
51+
const previousState: T = usePrevious<T>(state: T);
52+
```
53+
54+
</TabItem>
55+
56+
</Tabs>
57+
58+
### Params
59+
60+
| Property | Description | Type | Default |
61+
| -------- | ------------------------------------------------- | ----- | ------- |
62+
| value | The state/prop value that previous value required | `any` | - |

docs/docs/useSafeReducer.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# useSafeReducer
3+
4+
A memory safe version of react's `useSafeReducer` hook.
5+
6+
This hook will prevent memory leaks by checking the component is unmounted before the state update operation due to `dispatch` if the component is unmounted, it will jsut ignore the `dispatch` operation. The API is same as react's `useReducer` hook, so you can use this hook instead of `useReducer` for asynchronous opration to avoid any memory leak.
7+
8+
## Examples
9+
10+
### Basic usage
11+
12+
13+
14+
## API
15+
16+
```typescript
17+
const [state, dispatch] = useSafeReducer(initialState, reducer)
18+
```
19+

docs/docs/useSafeState.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# useSafeState
2+
3+
A memory safe version of react's `useState` hook. In react memory leak occurs when `setState` operation performed on an unmounted component. This happens mostly with asynchronous opration like api calls.
4+
5+
For example, if the user initiated an api call and navigated away from tha page before the call is returned, the component will get unmounted and when the api call is fulfilled, `setstate` will be performed on the unmounted component causing a memory leak.
6+
7+
This hook will prevent these kind of memory leaks by checking whether the component is mounted before `setstate` operation, if the component is unmounted, it will jsut ignore the `setstate` call. The API is same as react's `useState` hook, so you can use this hook instead of `useState` for asynchronous opration to avoid any memory leak.
8+
9+
## Usage
10+
11+
```typescript
12+
const [value, setvalue] = useSafeState(initialState);
13+
```
14+
15+
## Examples
16+
17+
```jsx live
18+
function SafeStateExample(props) {
19+
const [count, setCount] = useSafeState(0);
20+
21+
return (
22+
<div>
23+
Current value: <b>{count}</b>, Previous value: <b>{prevCount}</b>
24+
<br />
25+
<button onClick={() => setCount(count + 1)}>+ count</button>
26+
<button
27+
style={{ marginLeft: '10px' }}
28+
onClick={() => setCount(count - 1)}
29+
>
30+
- count
31+
</button>
32+
</div>
33+
);
34+
}
35+
```
36+
37+
## API
38+
39+
import Tabs from '@theme/Tabs';
40+
import TabItem from '@theme/TabItem';
41+
42+
<Tabs>
43+
<TabItem value="js" label="JavaScript">
44+
45+
```js
46+
const [state, setState] = useSafeState(initialState);
47+
```
48+
49+
</TabItem>
50+
<TabItem value="ts" label="Typescript">
51+
52+
```typescript
53+
const [state, setState] = useSafeState(initialState);
54+
```
55+
56+
</TabItem>
57+
58+
</Tabs>

docs/docs/useWhyDidYouUpdate.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# useWhyDidYouUpdate
3+
4+
Help developers troubleshoot what changes have caused component rerender.
5+
6+
## Examples
7+
8+
### Default usage
9+
10+
## API
11+
12+
```typescript
13+
type IProps = Record<string, any>;
14+
15+
useWhyDidYouUpdate(componentName: string, props: IProps): void;
16+
```
17+
18+
### Params
19+
20+
| Property | Description | Type | Default |
21+
|---------------|---------------------------------------------------------------------------------------------|----------|---------|
22+
| componentName | Required, the name of the observation component | `string` | - |
23+
| props | Required, data to be observed (`state` or `props` and other data that may lead to rerender) | `object` | - |
24+
25+
26+
### Result
27+
28+
Please open the browser console, you can see the output of the changed observed `state` or `props`.

0 commit comments

Comments
 (0)