-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor some components to match the
react-hooks/exhaustive-deps
e…
…slint rules (#25064)
- Loading branch information
1 parent
4b40677
commit 2e25f1c
Showing
10 changed files
with
464 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useLayoutEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* A drop-in replacement of the hook version of `componentDidMount`. | ||
* Like `useEffect` but only called once when the component is mounted. | ||
* This hook is only used for backward-compatibility reason. Consider using `useEffect` wherever possible. | ||
* | ||
* @param {Function} effect The effect callback passed to `useEffect`. | ||
*/ | ||
function useDidMount( effect ) { | ||
const effectRef = useRef( effect ); | ||
effectRef.current = effect; | ||
|
||
// `useLayoutEffect` because that's closer to how the `componentDidMount` works. | ||
useLayoutEffect( () => effectRef.current(), [] ); | ||
} | ||
|
||
export default useDidMount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import React, { Component, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useDidMount from '../'; | ||
|
||
describe( 'useDidMount', () => { | ||
it( 'should call the effect when did mount', () => { | ||
const mountEffect = jest.fn(); | ||
|
||
function TestComponent() { | ||
useDidMount( mountEffect ); | ||
return null; | ||
} | ||
|
||
render( <TestComponent /> ); | ||
|
||
expect( mountEffect ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
|
||
it( 'should call the cleanup function when unmount', () => { | ||
const unmountCallback = jest.fn(); | ||
|
||
function TestComponent() { | ||
useDidMount( () => unmountCallback ); | ||
return null; | ||
} | ||
|
||
const { unmount } = render( <TestComponent /> ); | ||
|
||
expect( unmountCallback ).not.toHaveBeenCalled(); | ||
|
||
unmount(); | ||
|
||
expect( unmountCallback ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
|
||
it( 'should match the calling order of componentDidMount', async () => { | ||
const mountEffectCallback = jest.fn(); | ||
const effectCallback = jest.fn(); | ||
|
||
const didMountCallback = jest.fn( | ||
() => | ||
new Promise( ( resolve ) => { | ||
expect( mountEffectCallback ).toHaveBeenCalled(); | ||
expect( effectCallback ).not.toHaveBeenCalled(); | ||
|
||
resolve(); | ||
} ) | ||
); | ||
|
||
let promise; | ||
|
||
class DidMount extends Component { | ||
componentDidMount() { | ||
promise = didMountCallback(); | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
function Hook() { | ||
useDidMount( mountEffectCallback ); | ||
useEffect( effectCallback ); | ||
return null; | ||
} | ||
|
||
function TestComponent() { | ||
return ( | ||
<> | ||
<Hook /> | ||
<DidMount /> | ||
</> | ||
); | ||
} | ||
|
||
render( <TestComponent /> ); | ||
|
||
await promise; | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
|
||
const INITIAL_TAG = Symbol( 'INITIAL_TAG' ); | ||
|
||
/** | ||
* Like `useRef` but only run the initializer once. | ||
* | ||
* @typedef {import('@types/react').MutableRefObject} MutableRefObject | ||
* | ||
* @param {Function} initializer A function to return the ref object. | ||
* @return {MutableRefObject} The returned ref object. | ||
*/ | ||
function useLazyRef( initializer ) { | ||
const ref = useRef( INITIAL_TAG ); | ||
|
||
if ( ref.current === INITIAL_TAG ) { | ||
ref.current = initializer(); | ||
} | ||
|
||
return ref; | ||
} | ||
|
||
export default useLazyRef; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import React, { useReducer } from '@wordpress/element'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { render, act } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useLazyRef from '..'; | ||
|
||
describe( 'useLazyRef', () => { | ||
it( 'should lazily initialize the initializer only once', () => { | ||
const initializer = jest.fn( () => 87 ); | ||
let result; | ||
let forceUpdate = () => {}; | ||
|
||
function TestComponent() { | ||
const ref = useLazyRef( initializer ); | ||
|
||
forceUpdate = useReducer( ( c ) => c + 1, 0 )[ 1 ]; | ||
|
||
result = ref.current; | ||
|
||
return null; | ||
} | ||
|
||
render( <TestComponent /> ); | ||
|
||
expect( initializer ).toHaveBeenCalledTimes( 1 ); | ||
expect( result ).toBe( 87 ); | ||
|
||
act( () => { | ||
forceUpdate(); | ||
} ); | ||
|
||
expect( initializer ).toHaveBeenCalledTimes( 1 ); | ||
expect( result ).toBe( 87 ); | ||
} ); | ||
|
||
it( 'should not accept falsy values', () => { | ||
const initializer = jest.fn( () => 87 ); | ||
let result; | ||
let ref = { current: null }; | ||
let forceUpdate = () => {}; | ||
|
||
function TestComponent() { | ||
ref = useLazyRef( initializer ); | ||
|
||
forceUpdate = useReducer( ( c ) => c + 1, 0 )[ 1 ]; | ||
|
||
result = ref.current; | ||
|
||
return null; | ||
} | ||
|
||
render( <TestComponent /> ); | ||
|
||
expect( initializer ).toHaveBeenCalledTimes( 1 ); | ||
expect( result ).toBe( 87 ); | ||
|
||
ref.current = undefined; | ||
act( () => { | ||
forceUpdate(); | ||
} ); | ||
|
||
expect( initializer ).toHaveBeenCalledTimes( 1 ); | ||
expect( result ).toBe( undefined ); | ||
|
||
ref.current = null; | ||
act( () => { | ||
forceUpdate(); | ||
} ); | ||
|
||
expect( initializer ).toHaveBeenCalledTimes( 1 ); | ||
expect( result ).toBe( null ); | ||
} ); | ||
} ); |
26 changes: 26 additions & 0 deletions
26
packages/compose/src/hooks/use-shallow-compare-effect/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
|
||
/** | ||
* Like `useEffect` but call the effect when the dependencies are not shallowly equal. | ||
* Useful when the size of the dependency array might change during re-renders. | ||
* This hook is only used for backward-compatibility reason. Consider using `useEffect` wherever possible. | ||
* | ||
* @param {Function} effect The effect callback passed to `useEffect`. | ||
* @param {Array} deps The dependency array that is compared against shallowly. | ||
*/ | ||
function useShallowCompareEffect( effect, deps ) { | ||
const ref = useRef(); | ||
|
||
if ( ! isShallowEqual( ref.current, deps ) ) { | ||
ref.current = deps; | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect( effect, [ ref.current ] ); | ||
} | ||
|
||
export default useShallowCompareEffect; |
Oops, something went wrong.