mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-08 11:56:41 +00:00
useEffect & custom hooks notes
This commit is contained in:
parent
7c8a1539f5
commit
4ff60b00b3
1 changed files with 36 additions and 0 deletions
|
@ -220,3 +220,39 @@ Hook used to create a state object.
|
||||||
```js
|
```js
|
||||||
const [state, setState] = useState(default);
|
const [state, setState] = useState(default);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `useEffect`
|
||||||
|
|
||||||
|
Hook used to trigger an action on each reder of the component.
|
||||||
|
|
||||||
|
```js
|
||||||
|
useEffect(() =>
|
||||||
|
// introduce side effect of the render
|
||||||
|
|
||||||
|
return () => {/* clean up side effect */}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Custom Hooks
|
||||||
|
|
||||||
|
```js
|
||||||
|
// hook definitions
|
||||||
|
const useCutomHook = () => {
|
||||||
|
// eventual state definitions
|
||||||
|
|
||||||
|
// eventual function definitions
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
return { obj1, obj2, ... };
|
||||||
|
}
|
||||||
|
|
||||||
|
const Component(){
|
||||||
|
// retrieve elements from the hook
|
||||||
|
const {
|
||||||
|
obj1,
|
||||||
|
obj2,
|
||||||
|
...
|
||||||
|
} = useCustomHook();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue