-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code structure to make it easier for adding new examples (#3…
…8799) Summary: Pull Request resolved: #38799 Refactor the performance comparison examples to make it easier to add new ones. Changelog: [General][Internal] - Code refactor Reviewed By: rshest Differential Revision: D47857126 fbshipit-source-id: fc8ffbcc94a23b896a53f599161f3bf6e9c2eaad
- Loading branch information
1 parent
93d9248
commit a8962ce
Showing
7 changed files
with
99 additions
and
32 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
File renamed without changes.
File renamed without changes.
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
62 changes: 62 additions & 0 deletions
62
...er/js/examples/Performance/performanceComparisonExamples/SetStateInWrongEffectExample2.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,62 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {ItemDataType} from '../components/itemData'; | ||
|
||
import * as React from 'react'; | ||
import ItemList from '../components/ItemList'; | ||
import {LIST_1000_ITEMS} from '../components/itemData'; | ||
|
||
const {useState, useEffect} = React; | ||
const ItemListMemo = React.memo(ItemList); | ||
|
||
function BadExample(props: {listData: ItemDataType[], filteredText: string}) { | ||
const {listData, filteredText} = props; | ||
const [visibleListData, setVisibleListData] = | ||
useState<ItemDataType[]>(listData); | ||
|
||
useEffect(() => { | ||
setVisibleListData( | ||
listData.filter(item => | ||
item.name.toUpperCase().includes(filteredText.toUpperCase()), | ||
), | ||
); | ||
}, [listData, filteredText]); | ||
|
||
return <ItemListMemo data={visibleListData} />; | ||
} | ||
|
||
function GoodExample(props: {listData: ItemDataType[], filteredText: string}) { | ||
const {listData, filteredText} = props; | ||
const visibleListData = listData.filter(item => | ||
item.name.toUpperCase().includes(filteredText.toUpperCase()), | ||
); | ||
|
||
return <ItemListMemo data={visibleListData} />; | ||
} | ||
|
||
function SetStateInWrongEffectBadExample(): React.Node { | ||
return <BadExample listData={LIST_1000_ITEMS} filteredText="f8" />; | ||
} | ||
|
||
function SetStateInWrongEffectGoodExample(): React.Node { | ||
return <GoodExample listData={LIST_1000_ITEMS} filteredText="f8" />; | ||
} | ||
|
||
export default { | ||
title: 'Re-render with unnecessary effect and state changes', | ||
description: | ||
'You may not need an effect in your component. State updates in unnecessary effects will trigger re-render that can be avoided.', | ||
Bad: SetStateInWrongEffectBadExample, | ||
Good: SetStateInWrongEffectGoodExample, | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/rn-tester/js/examples/Performance/performanceComparisonExamples/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,13 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
export {default as ReRenderWithNonPureChildExample} from './ReRenderWithNonPureChildExample'; | ||
export {default as ReRenderWithObjectPropExample} from './ReRenderWithObjectPropExample'; |