Skip to content

Commit 0bba1bf

Browse files
feat: create useLifecycleHelpers custom hook with lifecycle helpers signaturs
1 parent 9831dd9 commit 0bba1bf

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

src/index.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,76 @@
1-
import React, { Component } from 'react'
2-
import PropTypes from 'prop-types'
1+
import { useRef, useState, useEffect } from 'react'
32

4-
import styles from './styles.css'
3+
const useLifecycleHelpers = (state = {}, props = {}) => {
4+
const [prevState, setPrevState] = useState(state)
5+
const [prevProps, setPrevProps] = useState(props)
6+
7+
/**
8+
* useComponentDidMount() is invoked immediately after a component is mounted.
9+
this is a good place to instantiate the network request.
10+
11+
This method is a good place to set up any subscriptions.
12+
If you do that, don’t forget to unsubscribe in componentWillUnmount().
13+
14+
You may call setState() immediately in useComponentDidMount().
15+
It will trigger an extra rendering.
16+
* @param {Function} callback
17+
*/
18+
const useComponentDidMount = (callback) => {
19+
20+
}
21+
22+
/**
23+
* useComponentDidUpdate() is invoked immediately after updating occurs.
24+
This method is not called for the initial render.
25+
26+
Use this as an opportunity to operate on the DOM when the component has been updated.
27+
This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).
28+
* @param {Function} callback
29+
*/
30+
const useComponentDidUpdate = (
31+
callback
32+
) => {
533

6-
export default class ExampleComponent extends Component {
7-
static propTypes = {
8-
text: PropTypes.string
934
}
1035

11-
render() {
12-
const {
13-
text
14-
} = this.props
36+
/**
37+
* useOnDependenciesChange() is invoked immediately after one of the dependencies changed.
38+
* This is also a good place to do network requests as long as you compare the current dependencies to previous dependencies value
39+
* EXP : useOnDependenciesChange(
40+
prevState => {
41+
console.log('componentDidUpdate', state, prevState)
42+
},
43+
[state]
44+
)
45+
* @param {Function} callback
46+
* @param {Array} dependencies
47+
*/
48+
const useOnDependenciesChange = (
49+
callback,
50+
dependencies
51+
) => {
1552

16-
return (
17-
<div className={styles.test}>
18-
Example Component: {text}
19-
</div>
20-
)
53+
}
54+
55+
/**
56+
* useComponentWillUnmount() is invoked immediately before a component is unmounted and destroyed.
57+
* Perform any necessary cleanup in this method, such as invalidating timers,
58+
* canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().
59+
60+
* You should not call setState() in useComponentWillUnmount() because the component will never be re-rendered.
61+
* Once a component instance is unmounted, it will never be mounted again.
62+
* @param {Function} callback
63+
*/
64+
const useComponentWillUnmount = callback => {
65+
66+
}
67+
68+
return {
69+
useComponentDidMount,
70+
useOnDependenciesChange,
71+
useComponentDidUpdate,
72+
useComponentWillUnmount
2173
}
2274
}
75+
76+
export default useLifecycleHelpers

0 commit comments

Comments
 (0)