File size: 2,521 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# ahooks function specification

ahooks tries its best to help everyone avoid the closure problem by specially processing the input and output functions.

**1. All the output functions of ahooks, the references are stable**

```ts
const [state, setState] = useState();
```

As we all know, the reference of the `setState` function returned by `React.useState` is fixed, and there is no need to consider weird problems when using it, and there is no need to put `setState` in the dependencies of other Hooks.

All functions returned by ahooks Hooks have the same characteristics as `setState`, the reference will not change, just feel free to use it.

**2. For all user input functions, always use the latest one**

For the received function, ahooks will do a special process to ensure that the function called each time is always the latest.

```ts
const [state, setState] = useState();

useInterval(() => {
  console.log(state);
}, 1000);
```

For example, in the above example, the function called by `useInterval` at any time is always the latest, that is, the state is always the latest.

## Principle

For the input function, we use `useRef` to make a record to ensure that the latest function can be accessed anywhere.

```js
const fnRef = useRef(fn);
fnRef.current = fn;
```

For example, the useUnmount code is as follows:

```js
const useUnmount = (fn) => {
  const fnRef = useRef(fn);
  fnRef.current = fn;

  useEffect(
    () => () => {
      fnRef.current();
    },
    [],
  );
};
```

In the above code, because we use ref for memorizing the latest function to solve the closure problem.

For the output function, we use the [useMemoizedFn](/zh-CN/hooks/use-memoized-fn) wrapped to ensure that the reference address will never change.

For a simple example, given a `useToggle` Hook, the code is like this:

```js
const useToggle = (left, right) => {
  const [state, setState] = useState(left);

  const toggle = useCallback(() => {
    setState((s) => (s === left ? right : left));
  }, [left, right]);

  return [state, toggle];
};
```

The `toggle` function returned in this demo will change according to the changes of `left/right`, which is uncomfortable for users to use.

Then we replace `useCallback` with `useMemoizedFn` to realize that the `toggle` reference will never change.

```js
const useToggle = (left, right) => {
  const [state, setState] = useState(left);

  const toggle = useMemoizedFn(() => {
    setState((s) => (s === left ? right : left));
  });

  return [state, toggle];
};
```