Skip to content

Commit ad3738b

Browse files
committed
perf(Blocks,Block): add deep comparison to block/blocks memo call
React.memo uses a shallow comparison, which isn't enough in this case. Using [welldone-software/why-did-you-render](https://github.com/welldone-software/why-did-you-render) I found our homepage content would update 12 times in one load with the wdyr message "different objects that are equal by value". This change adds a deep comparison, which when tested via patch-package, eliminated those re-renders.
1 parent 8bcaa92 commit ad3738b

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

.changeset/fair-islands-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@builder.io/sdk-react-native": patch
3+
---
4+
5+
Added deep memo comparison using react-fast-compare.

packages/sdks/mitosis.config.cjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,20 @@ const MEMOIZING_BLOCKS_COMPONENT_PLUGIN = () => ({
174174
imports: { memo: 'memo' },
175175
path: 'react',
176176
});
177+
json.imports.push({
178+
imports: { isEqual: 'default' },
179+
path: 'react-fast-compare',
180+
});
177181
}
178182
if (json.name === 'Blocks') {
179183
json.imports.push({
180184
imports: { memo: 'memo' },
181185
path: 'react',
182186
});
187+
json.imports.push({
188+
imports: { isEqual: 'default' },
189+
path: 'react-fast-compare',
190+
});
183191

184192
json.hooks.init = {
185193
code: `
@@ -240,13 +248,13 @@ const MEMOIZING_BLOCKS_COMPONENT_PLUGIN = () => ({
240248
if (json.name === 'Blocks') {
241249
return code.replace(
242250
'export default Blocks',
243-
'export default memo(Blocks)'
251+
'export default memo(Blocks, isEqual)'
244252
);
245253
}
246254
if (json.name === 'Block') {
247255
return code.replace(
248256
'export default Block',
249-
'export default memo(Block)'
257+
'export default memo(Block, isEqual)'
250258
);
251259
}
252260
return code;

packages/sdks/output/react-native/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"@react-native-async-storage/async-storage": "1.23.1",
112112
"css-to-react-native": "^3.2.0",
113113
"isolated-vm": "^6.0.0",
114+
"react-fast-compare": "^3.2.2",
114115
"react-native-storage": "^1.0.1",
115116
"react-native-video": "^5.1.1"
116117
},

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7093,6 +7093,7 @@ __metadata:
70937093
babel-plugin-module-resolver: ^5.0.0
70947094
css-to-react-native: ^3.2.0
70957095
isolated-vm: ^6.0.0
7096+
react-fast-compare: ^3.2.2
70967097
react-native-builder-bob: ^0.21.3
70977098
react-native-storage: ^1.0.1
70987099
react-native-url-polyfill: ^2.0.0

0 commit comments

Comments
 (0)