Skip to content

Commit a8962ce

Browse files
Xin Chenfacebook-github-bot
Xin Chen
authored andcommitted
Refactor code structure to make it easier for adding new examples (#38799)
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
1 parent 93d9248 commit a8962ce

File tree

7 files changed

+99
-32
lines changed

7 files changed

+99
-32
lines changed

packages/rn-tester/js/examples/Performance/PerformanceComparisonExample.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import * as React from 'react';
1515
import {StyleSheet, View, Text} from 'react-native';
1616
import RNTesterPage from '../../components/RNTesterPage';
1717
import RNTesterButton from '../../components/RNTesterButton';
18-
import ReRenderWithObjectPropExample from './ReRenderWithObjectPropExample';
19-
import ReRenderWithNonPureChildExample from './ReRenderWithNonPureChildExample';
18+
import * as performanceComparisonExamples from './performanceComparisonExamples';
19+
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
2020

2121
const {useState, useCallback, useMemo} = React;
2222
const SHOW_NOTHING = 'SHOW_NOTHING';
@@ -114,29 +114,21 @@ exports.title = 'Performance Comparison Examples';
114114
exports.category = 'Basic';
115115
exports.description =
116116
'Compare performance with bad and good examples. Use React DevTools to highlight re-renders is recommended.';
117-
exports.examples = [
118-
{
119-
title: ReRenderWithNonPureChildExample.title,
120-
description: ReRenderWithNonPureChildExample.description,
121-
render: function (): React.Node {
122-
return (
123-
<PerfExampleWrapper
124-
badExample={<ReRenderWithNonPureChildExample.Bad />}
125-
goodExample={<ReRenderWithNonPureChildExample.Good />}
126-
/>
127-
);
128-
},
129-
},
130-
{
131-
title: ReRenderWithObjectPropExample.title,
132-
description: ReRenderWithObjectPropExample.description,
133-
render: function (): React.Node {
134-
return (
135-
<PerfExampleWrapper
136-
badExample={<ReRenderWithObjectPropExample.Bad />}
137-
goodExample={<ReRenderWithObjectPropExample.Good />}
138-
/>
139-
);
140-
},
141-
},
142-
];
117+
118+
const examples: Array<RNTesterModuleExample> = Object.keys(
119+
performanceComparisonExamples,
120+
).map(name => {
121+
const example = performanceComparisonExamples[name];
122+
return {
123+
title: example.title,
124+
description: example.description,
125+
render: () => (
126+
<PerfExampleWrapper
127+
badExample={<example.Bad />}
128+
goodExample={<example.Good />}
129+
/>
130+
),
131+
};
132+
});
133+
134+
exports.examples = examples;

packages/rn-tester/js/examples/Performance/ReRenderWithNonPureChildExample.js renamed to packages/rn-tester/js/examples/Performance/performanceComparisonExamples/ReRenderWithNonPureChildExample.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import * as React from 'react';
1515
import {Text} from 'react-native';
16-
import ItemList from './ItemList';
17-
import {LIST_100_ITEMS} from './itemData';
16+
import ItemList from '../components/ItemList';
17+
import {LIST_100_ITEMS} from '../components/itemData';
1818
import type {ScrollEvent} from 'react-native/Libraries/Types/CoreEventTypes';
1919

2020
const {useCallback, useState} = React;

packages/rn-tester/js/examples/Performance/ReRenderWithObjectPropExample.js renamed to packages/rn-tester/js/examples/Performance/performanceComparisonExamples/ReRenderWithObjectPropExample.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import * as React from 'react';
1515
import {Text} from 'react-native';
16-
import ItemList from './ItemList';
17-
import {LIST_100_ITEMS} from './itemData';
16+
import ItemList from '../components/ItemList';
17+
import {LIST_100_ITEMS} from '../components/itemData';
1818
import type {ScrollEvent} from 'react-native/Libraries/Types/CoreEventTypes';
1919

2020
const {useState, useCallback} = React;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
'use strict';
13+
14+
import type {ItemDataType} from '../components/itemData';
15+
16+
import * as React from 'react';
17+
import ItemList from '../components/ItemList';
18+
import {LIST_1000_ITEMS} from '../components/itemData';
19+
20+
const {useState, useEffect} = React;
21+
const ItemListMemo = React.memo(ItemList);
22+
23+
function BadExample(props: {listData: ItemDataType[], filteredText: string}) {
24+
const {listData, filteredText} = props;
25+
const [visibleListData, setVisibleListData] =
26+
useState<ItemDataType[]>(listData);
27+
28+
useEffect(() => {
29+
setVisibleListData(
30+
listData.filter(item =>
31+
item.name.toUpperCase().includes(filteredText.toUpperCase()),
32+
),
33+
);
34+
}, [listData, filteredText]);
35+
36+
return <ItemListMemo data={visibleListData} />;
37+
}
38+
39+
function GoodExample(props: {listData: ItemDataType[], filteredText: string}) {
40+
const {listData, filteredText} = props;
41+
const visibleListData = listData.filter(item =>
42+
item.name.toUpperCase().includes(filteredText.toUpperCase()),
43+
);
44+
45+
return <ItemListMemo data={visibleListData} />;
46+
}
47+
48+
function SetStateInWrongEffectBadExample(): React.Node {
49+
return <BadExample listData={LIST_1000_ITEMS} filteredText="f8" />;
50+
}
51+
52+
function SetStateInWrongEffectGoodExample(): React.Node {
53+
return <GoodExample listData={LIST_1000_ITEMS} filteredText="f8" />;
54+
}
55+
56+
export default {
57+
title: 'Re-render with unnecessary effect and state changes',
58+
description:
59+
'You may not need an effect in your component. State updates in unnecessary effects will trigger re-render that can be avoided.',
60+
Bad: SetStateInWrongEffectBadExample,
61+
Good: SetStateInWrongEffectGoodExample,
62+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
export {default as ReRenderWithNonPureChildExample} from './ReRenderWithNonPureChildExample';
13+
export {default as ReRenderWithObjectPropExample} from './ReRenderWithObjectPropExample';

0 commit comments

Comments
 (0)