-
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.
perf: Reduce unnecessary LinkPicker re-renders (#51613)
* perf: Reduce unnecessary LinkPicker re-renders Combining state with an object in a single `useState` meant a new object was set to the state regardless of whether new values were present. This resulted in unexpected `act` warnings caused by unnecessary re-renders. Refactoring LinkPicker to separate the two state values into separate `useState` calls resolves this issue by using primitives that can be compared directly, which results in `useState` not triggering re-renders when the values are the same. * refactor: Simplify component callback invocations The wrapping anonymous function is unnecessary now that the callback are simply setting a single state value. Also, passing anonymous functions to child components can lead to unnecessary re-renders. * build: Remove erroneous reassure argument This argument does not exist in the older version of reassure used. Also, the value is already passed as an environment variable. * test: Add LinkPicker performance test
- Loading branch information
Showing
4 changed files
with
43 additions
and
15 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
35 changes: 35 additions & 0 deletions
35
packages/components/src/mobile/link-picker/test/performance/index.native.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,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { act, measurePerformance } from 'test/helpers'; | ||
import Clipboard from '@react-native-clipboard/clipboard'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { LinkPicker } from '../../index'; | ||
|
||
describe( 'LinkPicker', () => { | ||
const onLinkPicked = jest.fn(); | ||
const onCancel = jest.fn(); | ||
const clipboardResult = Promise.resolve( '' ); | ||
Clipboard.getString.mockReturnValue( clipboardResult ); | ||
|
||
it( 'performance is stable when clipboard results do not change', async () => { | ||
const scenario = async () => { | ||
// Given the clipboard result is an empty string, there are no | ||
// user-facing changes to query. Thus, we must await the promise | ||
// itself. | ||
await act( () => clipboardResult ); | ||
}; | ||
|
||
await measurePerformance( | ||
<LinkPicker | ||
onLinkPicked={ onLinkPicked } | ||
onCancel={ onCancel } | ||
value="" | ||
/>, | ||
{ scenario } | ||
); | ||
} ); | ||
} ); |
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