-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-i18n perf optimizations (#659)
- Loading branch information
Showing
15 changed files
with
261 additions
and
91 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {useLazyRef} from './lazy-ref'; | ||
export {default as useTimeout} from './timeout'; | ||
export {default as useOnValueChange} from './on-value-change'; |
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,13 @@ | ||
import {useRef, MutableRefObject} from 'react'; | ||
|
||
const UNSET = Symbol('unset'); | ||
|
||
export function useLazyRef<T>(getValue: () => T): MutableRefObject<T> { | ||
const ref = useRef<T | typeof UNSET>(UNSET); | ||
|
||
if (ref.current === UNSET) { | ||
ref.current = getValue(); | ||
} | ||
|
||
return ref as MutableRefObject<T>; | ||
} |
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 * as React from 'react'; | ||
import {mount} from '@shopify/react-testing'; | ||
|
||
import {useLazyRef} from '../lazy-ref'; | ||
|
||
describe('useLazyRef()', () => { | ||
it('calls the passed function and returns the ref for it', () => { | ||
const content = 'Hello world'; | ||
const spy = jest.fn(() => content); | ||
|
||
function MockComponent() { | ||
const value = useLazyRef(spy); | ||
return <div>{value.current}</div>; | ||
} | ||
|
||
const mockComponent = mount(<MockComponent />); | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
expect(mockComponent).toContainReactText(content); | ||
}); | ||
|
||
it('does not call the passed function on subsequent renders', () => { | ||
function MockComponent({spy}: {spy: () => any}) { | ||
const value = useLazyRef(spy); | ||
return <div>{value.current}</div>; | ||
} | ||
|
||
const mockComponent = mount(<MockComponent spy={() => 'Hello'} />); | ||
|
||
const newContent = 'Goodbye'; | ||
const spy = jest.fn(() => newContent); | ||
mockComponent.setProps({spy}); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
expect(mockComponent).not.toContainReactText(newContent); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.