Skip to content

Commit 51b2638

Browse files
authored
Fix performance issues (#370)
1 parent 3e772e2 commit 51b2638

File tree

7 files changed

+49
-50
lines changed

7 files changed

+49
-50
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
},
2323
"dependencies": {
2424
"object-hash": "2.2.0",
25-
"react-fast-compare": "3.2.0",
2625
"shortid": "2.2.16",
2726
"use-global-hook": "0.3.0"
2827
},
@@ -33,8 +32,8 @@
3332
"devDependencies": {
3433
"@babel/core": "7.12.9",
3534
"@babel/runtime": "7.12.5",
36-
"@commitlint/cli": "^16.1.0",
37-
"@commitlint/config-conventional": "^16.0.0",
35+
"@commitlint/cli": "16.1.0",
36+
"@commitlint/config-conventional": "16.0.0",
3837
"@react-native-community/eslint-config": "3.0.1",
3938
"@testing-library/jest-native": "4.0.4",
4039
"@testing-library/react-native": "9.0.0",
@@ -49,7 +48,7 @@
4948
"codecov": "3.8.3",
5049
"eslint": "7.32.0",
5150
"eslint-plugin-prettier": "4.0.0",
52-
"husky": "^7.0.4",
51+
"husky": "7.0.4",
5352
"istanbul": "0.4.5",
5453
"istanbul-api": "3.0.0",
5554
"istanbul-reports": "3.1.3",

src/NestedListView.test.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import React from 'react';
22
import { Text, View } from 'react-native';
33
import { INode } from './NodeView';
44
import { render, waitFor, fireEvent } from '@testing-library/react-native';
@@ -307,18 +307,21 @@ describe('NestedListView', () => {
307307

308308
const mockOnNodePressed = jest.fn();
309309

310-
const { UNSAFE_queryAllByType } = render(
310+
const { queryByText } = render(
311311
<NestedListView
312312
onNodePressed={mockOnNodePressed}
313313
renderNode={(node: INode, level?: number) => (
314-
<NestedRow level={level}>{node.name}</NestedRow>
314+
<NestedRow level={level}>
315+
<Text>{node.title}</Text>
316+
</NestedRow>
315317
)}
316318
data={data}
317319
/>,
318320
);
319321

320-
const component = UNSAFE_queryAllByType(NestedRow);
321-
expect(component.length).toEqual(3);
322+
expect(queryByText('child1')).toBeTruthy();
323+
expect(queryByText('child2')).toBeTruthy();
324+
expect(queryByText('child3')).toBeTruthy();
322325
});
323326

324327
test('renders without renderNode', async () => {

src/NestedListView.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import hashObjectGenerator from 'object-hash';
22
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
3-
import isEqual from 'react-fast-compare';
43
import { StyleSheet, Text, View } from 'react-native';
54
import shortid from 'shortid';
65
import { INode, NodeView } from './NodeView';
@@ -64,9 +63,7 @@ const NestedListView = React.memo(
6463
};
6564
}
6665

67-
const childrenName: string = getChildrenName
68-
? getChildrenName(node)
69-
: 'items';
66+
const childrenName = getChildrenName ? getChildrenName(node) : 'items';
7067
let children = node[childrenName];
7168

7269
if (children) {
@@ -133,13 +130,16 @@ const NestedListView = React.memo(
133130
generateRootNode,
134131
]);
135132

136-
const _getChildrenName = (node: INode) => {
137-
if (node.name === 'root') {
138-
return 'items';
139-
}
133+
const _getChildrenName = React.useCallback(
134+
(node: INode) => {
135+
if (node.name === 'root') {
136+
return 'items';
137+
}
140138

141-
return getChildrenName ? getChildrenName(node) : 'items';
142-
};
139+
return getChildrenName ? getChildrenName(node) : 'items';
140+
},
141+
[getChildrenName],
142+
);
143143

144144
const renderErrorMessage = (prop: string) => {
145145
return (
@@ -169,7 +169,8 @@ const NestedListView = React.memo(
169169
/>
170170
);
171171
},
172-
isEqual,
173172
);
174173

174+
NestedListView.displayName = 'NestedListView';
175+
175176
export { NestedListView };

src/NestedRow.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
import * as React from 'react';
1+
import React from 'react';
22
import { Text } from 'react-native';
33
import { NestedRow } from './NestedRow';
44
import { render, waitFor } from '@testing-library/react-native';
55

66
describe('NestedListView', () => {
77
test('renders with succesfully', async () => {
8-
const { UNSAFE_getByType } = render(
8+
const { getByText } = render(
99
<NestedRow level={1} style={{ borderColor: 'black', borderWidth: 1 }}>
1010
<Text>Test</Text>
1111
</NestedRow>,
1212
);
1313

1414
await waitFor(() => {
15-
const component = UNSAFE_getByType(NestedRow);
16-
expect(component).toBeDefined();
15+
const component = getByText('Test');
16+
expect(component).toBeTruthy();
1717
});
1818
});
1919

2020
test('renders without level passed', async () => {
21-
const { UNSAFE_getByType } = render(
21+
const { getByText } = render(
2222
<NestedRow style={{ borderColor: 'black', borderWidth: 1 }}>
2323
<Text>Test</Text>
2424
</NestedRow>,
2525
);
2626

2727
await waitFor(() => {
28-
const component = UNSAFE_getByType(NestedRow);
29-
expect(component).toBeDefined();
28+
const component = getByText('Test');
29+
expect(component).toBeTruthy();
3030
});
3131
});
3232

3333
test('renders with height passed', async () => {
34-
const { UNSAFE_getByType } = render(
34+
const { getByText } = render(
3535
<NestedRow
3636
level={1}
3737
height={60}
@@ -42,8 +42,8 @@ describe('NestedListView', () => {
4242
);
4343

4444
await waitFor(() => {
45-
const component = UNSAFE_getByType(NestedRow);
46-
expect(component).toBeDefined();
45+
const component = getByText('Test');
46+
expect(component).toBeTruthy();
4747
});
4848
});
4949
});

src/NestedRow.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as React from 'react';
2-
import isEqual from 'react-fast-compare';
1+
import React, { useMemo } from 'react';
32
import { StyleSheet, View } from 'react-native';
43

54
const styles = StyleSheet.create({
@@ -24,20 +23,22 @@ const NestedRow = React.memo(
2423
level = 0,
2524
paddingLeftIncrement = 10,
2625
style,
27-
}: IProps) => (
28-
<View
29-
style={[
26+
}: IProps) => {
27+
const composedStyles = useMemo(
28+
() => [
3029
styles.nestedRow,
3130
{
3231
...style,
3332
paddingLeft: level * paddingLeftIncrement,
3433
},
3534
height ? { height } : {},
36-
]}>
37-
{children}
38-
</View>
39-
),
40-
isEqual,
35+
],
36+
[height, level, paddingLeftIncrement, style],
37+
);
38+
return <View style={composedStyles}>{children}</View>;
39+
},
4140
);
4241

42+
NestedRow.displayName = 'NestedRow';
43+
4344
export { NestedRow };

src/NodeView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
2-
import isEqual from 'react-fast-compare';
32
import { FlatList, Pressable } from 'react-native';
43
import globalHook, { Store } from 'use-global-hook';
54

@@ -147,7 +146,8 @@ const NodeView = React.memo(
147146
</>
148147
);
149148
},
150-
isEqual,
151149
);
152150

151+
NodeView.displayName = 'NodeView';
152+
153153
export { NodeView };

yarn.lock

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@
794794
exec-sh "^0.3.2"
795795
minimist "^1.2.0"
796796

797-
"@commitlint/cli@^16.1.0":
797+
"@commitlint/cli@16.1.0":
798798
version "16.1.0"
799799
resolved "https://registry.yarnpkg.com/@commitlint/cli/-/cli-16.1.0.tgz#022ad86008374b02974c9f3faf86affb785f4574"
800800
integrity sha512-x5L1knvA3isRWBRVQx+Q6D45pA9139a2aZQYpxkljMG0dj4UHZkCnsYWpnGalxPxASI7nrI0KedKfS2YeQ55cQ==
@@ -809,7 +809,7 @@
809809
resolve-global "1.0.0"
810810
yargs "^17.0.0"
811811

812-
"@commitlint/config-conventional@^16.0.0":
812+
"@commitlint/config-conventional@16.0.0":
813813
version "16.0.0"
814814
resolved "https://registry.yarnpkg.com/@commitlint/config-conventional/-/config-conventional-16.0.0.tgz#f42d9e1959416b5e691c8b5248fc2402adb1fc03"
815815
integrity sha512-mN7J8KlKFn0kROd+q9PB01sfDx/8K/R25yITspL1No8PB4oj9M1p77xWjP80hPydqZG9OvQq+anXK3ZWeR7s3g==
@@ -4121,7 +4121,7 @@ human-signals@^2.1.0:
41214121
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
41224122
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==
41234123

4124-
husky@^7.0.4:
4124+
husky@7.0.4:
41254125
version "7.0.4"
41264126
resolved "https://registry.yarnpkg.com/husky/-/husky-7.0.4.tgz#242048245dc49c8fb1bf0cc7cfb98dd722531535"
41274127
integrity sha512-vbaCKN2QLtP/vD4yvs6iz6hBEo6wkSzs8HpRah1Z6aGmF2KW5PdYuAd7uX5a+OyBZHBhd+TFLqgjUgytQr4RvQ==
@@ -6535,11 +6535,6 @@ react-devtools-core@4.19.1:
65356535
shell-quote "^1.6.1"
65366536
ws "^7"
65376537

6538-
react-fast-compare@3.2.0:
6539-
version "3.2.0"
6540-
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
6541-
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==
6542-
65436538
"react-is@^16.12.0 || ^17.0.0", react-is@^17.0.1, react-is@^17.0.2:
65446539
version "17.0.2"
65456540
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"

0 commit comments

Comments
 (0)