Skip to content

Commit 0f4e0de

Browse files
committed
feat(theme): memoize createStyleProp
Apparently StyleConsumerService's createStyleProp is quite slow. Memoize it is not a silver bullet, but give you a chance to not generate those styles on each rerender by providing props to your styled component with a stable identity.
1 parent ea4a362 commit 0f4e0de

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/components/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
"dependencies": {
2424
"@eva-design/dss": "^2.0.0",
2525
"@eva-design/processor": "^2.0.0",
26+
"fecha": "3.0.3",
2627
"hoist-non-react-statics": "^3.2.1",
2728
"lodash.merge": "^4.6.1",
28-
"fecha": "3.0.3"
29+
"memoize-one": "^5.1.1",
30+
"react-fast-compare": "^3.2.0"
2931
},
3032
"peerDependencies": {
3133
"react-native-svg": "^9.13.6"

src/components/theme/style/styled.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
import React from 'react';
8+
import isEqual from 'react-fast-compare';
9+
import memoizeOne from 'memoize-one';
810
import hoistNonReactStatics from 'hoist-non-react-statics';
911
import { ThemeStyleType } from '@eva-design/dss';
1012
import { StyleConsumerService } from './styleConsumer.service';
@@ -110,13 +112,14 @@ const styleInjector = (Component: WrappedComponent, name: string): StyledCompone
110112
private init: boolean = false;
111113
private defaultProps: WrappedComponentProps;
112114
private service: StyleConsumerService;
115+
private createStyleProp: StyleConsumerService['createStyleProp']
113116

114117
private onInit = (style: ThemeStyleType, theme: ThemeType): void => {
115118
this.service = new StyleConsumerService(name, style);
116119
this.defaultProps = this.service.createDefaultProps();
120+
this.createStyleProp = memoizeOne(this.service.createStyleProp.bind(this.service), isEqual)
117121
this.init = true;
118122
};
119-
120123
private onDispatch = (interaction: Interaction[]): void => {
121124
this.setState({ interaction });
122125
};
@@ -126,7 +129,7 @@ const styleInjector = (Component: WrappedComponent, name: string): StyledCompone
126129
theme: ThemeType): StyledComponentProps => {
127130

128131
const props: WrappedComponentProps = { ...this.defaultProps, ...sourceProps };
129-
const style: StyleType = this.service.createStyleProp(props, sourceStyle, theme, this.state.interaction);
132+
const style: StyleType = this.createStyleProp(props, sourceStyle, theme, this.state.interaction);
130133

131134
return {
132135
...props,

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10105,6 +10105,11 @@ mem@^4.0.0:
1010510105
mimic-fn "^2.0.0"
1010610106
p-is-promise "^2.0.0"
1010710107

10108+
memoize-one@^5.1.1:
10109+
version "5.1.1"
10110+
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0"
10111+
integrity sha512-HKeeBpWvqiVJD57ZUAsJNm71eHTykffzcLZVYWiVfQeI1rJtuEaS7hQiEpWfVVk18donPwJEcFKIkCmPJNOhHA==
10112+
1010810113
memory-fs@^0.4.0, memory-fs@^0.4.1, memory-fs@~0.4.1:
1010910114
version "0.4.1"
1011010115
resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
@@ -12741,6 +12746,11 @@ react-error-overlay@^6.0.1:
1274112746
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.7.tgz#1dcfb459ab671d53f660a991513cb2f0a0553108"
1274212747
integrity sha512-TAv1KJFh3RhqxNvhzxj6LeT5NWklP6rDr2a0jaTfsZ5wSZWHOGeqQyejUp3xxLfPt2UpyJEcVQB/zyPcmonNFA==
1274312748

12749+
react-fast-compare@^3.2.0:
12750+
version "3.2.0"
12751+
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
12752+
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==
12753+
1274412754
react-is@^16.12.0, react-is@^16.13.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.9.0:
1274512755
version "16.13.1"
1274612756
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"

0 commit comments

Comments
 (0)