-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2166 from Shopify/add-lazy-ref
Add useLazyRef hook
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import {mount} from 'test-utilities'; | ||
import {useLazyRef} from '../use-lazy-ref'; | ||
|
||
describe('useLazyRef', () => { | ||
it('returns a ref object', () => { | ||
const spy = jest.fn(); | ||
|
||
function MockComponent() { | ||
const lazyValue = useLazyRef(() => true); | ||
spy(lazyValue); | ||
return null; | ||
} | ||
|
||
mount(<MockComponent />); | ||
expect(spy).toHaveBeenCalledWith({current: true}); | ||
}); | ||
|
||
it('only calls initialValue once', () => { | ||
const spy = jest.fn(); | ||
|
||
function MockComponent() { | ||
const [, setFooState] = useState(false); | ||
|
||
useEffect(() => { | ||
setFooState(true); | ||
}, []); | ||
|
||
useLazyRef(spy); | ||
|
||
return null; | ||
} | ||
|
||
mount(<MockComponent />); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,22 @@ | ||
import {useRef, MutableRefObject} from 'react'; | ||
|
||
const UNIQUE_IDENTIFIER = Symbol('unique_identifier'); | ||
|
||
/** | ||
* useLazyRef provides a lazy initial value, similar to lazy | ||
* initial state the initialValue is the value used during | ||
* initialization and disregarded after that. | ||
* @param initialValue - A function that will return the initial | ||
* value and be disregarded after that | ||
* @returns MutableRefObject<T> - Returns a ref object with the | ||
* results from invoking initial value | ||
*/ | ||
export function useLazyRef<T>(initialValue: () => T) { | ||
const lazyRef = useRef<T | Symbol>(UNIQUE_IDENTIFIER); | ||
|
||
if (lazyRef.current === UNIQUE_IDENTIFIER) { | ||
lazyRef.current = initialValue(); | ||
} | ||
|
||
return lazyRef as MutableRefObject<T>; | ||
} |