Skip to content

Commit 1e5e984

Browse files
refactor: change helpers name to make them shorter and easy to use
1 parent 2886477 commit 1e5e984

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ const useLifecycleHelpers = (state = {}, props = {}) => {
55
const [prevProps, setPrevProps] = useState(props)
66

77
/**
8-
* useComponentDidMount() is invoked immediately after a component is mounted.
8+
* useDidMount() is invoked immediately after a component is mounted.
99
this is a good place to instantiate the network request.
1010
1111
This method is a good place to set up any subscriptions.
1212
If you do that, don’t forget to unsubscribe in componentWillUnmount().
1313
14-
You may call setState() immediately in useComponentDidMount().
14+
You may call setState() immediately in useDidMount().
1515
It will trigger an extra rendering.
1616
* @param {Function} callback
1717
*/
18-
const useComponentDidMount = (callback) => {
18+
const useDidMount = (callback) => {
1919
useEffect(() => {
2020
callback()
2121
// eslint-disable-next-line
2222
}, [])
2323
}
2424

2525
/**
26-
* useComponentDidUpdate() is invoked immediately after updating occurs.
26+
* useDidUpdate() is invoked immediately after updating occurs.
2727
This method is not called for the initial render.
2828
2929
Use this as an opportunity to operate on the DOM when the component has been updated.
3030
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).
3131
* @param {Function} callback
3232
*/
33-
const useComponentDidUpdate = (
33+
const useDidUpdate = (
3434
callback
3535
) => {
3636
const isFirstRender = useRef(true)
@@ -48,9 +48,9 @@ const useLifecycleHelpers = (state = {}, props = {}) => {
4848
}
4949

5050
/**
51-
* useOnDependenciesChange() is invoked immediately after one of the dependencies changed.
51+
* useDepsDidChange() is invoked immediately after one of the dependencies changed.
5252
* This is also a good place to do network requests as long as you compare the current dependencies to previous dependencies value
53-
* EXP : useOnDependenciesChange(
53+
* EXP : useDepsDidChange(
5454
prevState => {
5555
console.log('componentDidUpdate', state, prevState)
5656
},
@@ -59,7 +59,7 @@ const useLifecycleHelpers = (state = {}, props = {}) => {
5959
* @param {Function} callback
6060
* @param {Array} dependencies
6161
*/
62-
const useOnDependenciesChange = (
62+
const useDepsDidChange = (
6363
callback,
6464
dependencies
6565
) => {
@@ -81,15 +81,15 @@ const useLifecycleHelpers = (state = {}, props = {}) => {
8181
}
8282

8383
/**
84-
* useComponentWillUnmount() is invoked immediately before a component is unmounted and destroyed.
84+
* useWillUnmount() is invoked immediately before a component is unmounted and destroyed.
8585
* Perform any necessary cleanup in this method, such as invalidating timers,
8686
* canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().
8787
88-
* You should not call setState() in useComponentWillUnmount() because the component will never be re-rendered.
88+
* You should not call setState() in useWillUnmount() because the component will never be re-rendered.
8989
* Once a component instance is unmounted, it will never be mounted again.
9090
* @param {Function} callback
9191
*/
92-
const useComponentWillUnmount = callback => {
92+
const useWillUnmount = callback => {
9393
useEffect(() => {
9494
return () => {
9595
callback()
@@ -99,10 +99,10 @@ const useLifecycleHelpers = (state = {}, props = {}) => {
9999
}
100100

101101
return {
102-
useComponentDidMount,
103-
useOnDependenciesChange,
104-
useComponentDidUpdate,
105-
useComponentWillUnmount
102+
useDidMount,
103+
useDepsDidChange,
104+
useDidUpdate,
105+
useWillUnmount
106106
}
107107
}
108108

src/test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ const Component = props => {
1919
const [state, setState] = useState({ counter: 0 })
2020

2121
const {
22-
useComponentDidMount,
23-
useComponentDidUpdate,
24-
useComponentWillUnmount,
25-
useOnDependenciesChange
22+
useDidMount,
23+
useDidUpdate,
24+
useWillUnmount,
25+
useDepsDidChange
2626
} = useLifecycleHelpers(state, props)
2727

28-
useComponentDidMount(() => {
28+
useDidMount(() => {
2929
setComponentState('Mounted')
3030
})
3131

32-
useComponentDidUpdate((prevState, prevProps) => {
32+
useDidUpdate((prevState, prevProps) => {
3333
if (props.data !== prevProps.data) setComponentState('Updated')
3434
})
3535

36-
useOnDependenciesChange(
36+
useDepsDidChange(
3737
prevState => {
3838
if (state.counter !== prevState.counter) setComponentState('DepsUpdated')
3939
},
4040
['counter']
4141
)
4242

43-
useComponentWillUnmount(() => {
43+
useWillUnmount(() => {
4444
mockCleanupCallback()
4545
})
4646

@@ -95,4 +95,4 @@ describe('Test useLifecycleHelpers custom hook', () => {
9595
expect(component).toBe(null)
9696
expect(mockCleanupCallback.mock.calls.length).toBe(1)
9797
})
98-
})
98+
})

0 commit comments

Comments
 (0)