Skip to content

Commit 5b1638e

Browse files
author
Vlad
committed
Remove deprecated lifecycle methods and reduce rerendering
1 parent d4a4841 commit 5b1638e

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shoutem/theme",
3-
"version": "0.11.1",
3+
"version": "0.11.4",
44
"main": "index.js",
55
"description": "Style your components in one place.",
66
"dependencies": {
@@ -15,7 +15,7 @@
1515
},
1616
"devDependencies": {
1717
"babel": "^6.3.26",
18-
"babel-preset-react-native": "^1.9.0",
18+
"babel-preset-react-native": "^5.0.1",
1919
"chai": "^3.4.1",
2020
"deep-freeze": "0.0.1",
2121
"enzyme": "^2.0.0-rc1",

src/StyleProvider.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { Children } from 'react';
1+
import React, { PureComponent, Children } from 'react';
22
import PropTypes from 'prop-types';
33
import Theme, { ThemeShape } from './Theme';
44

55
/**
66
* Provides a theme to child components trough context.
77
*/
8-
export default class StyleProvider extends React.Component {
8+
export default class StyleProvider extends PureComponent {
99
static propTypes = {
1010
children: PropTypes.element.isRequired,
1111
style: PropTypes.object,
@@ -19,10 +19,18 @@ export default class StyleProvider extends React.Component {
1919
theme: ThemeShape.isRequired,
2020
};
2121

22+
static getDerivedStateFromProps(props, state) {
23+
return props.style === state.style ? state : {
24+
style: props.style,
25+
theme: new Theme(props.style),
26+
};
27+
}
28+
2229
constructor(props, context) {
2330
super(props, context);
31+
2432
this.state = {
25-
theme: this.createTheme(props),
33+
theme: new Theme(props.style),
2634
};
2735
}
2836

@@ -32,18 +40,6 @@ export default class StyleProvider extends React.Component {
3240
};
3341
}
3442

35-
componentWillReceiveProps(nextProps) {
36-
if (nextProps.style !== this.props.style) {
37-
this.setState({
38-
theme: this.createTheme(nextProps),
39-
});
40-
}
41-
}
42-
43-
createTheme(props) {
44-
return new Theme(props.style);
45-
}
46-
4743
render() {
4844
const { children } = this.props;
4945

src/connectStyle.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from 'react';
1+
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33
import hoistStatics from 'hoist-non-react-statics';
4-
import * as _ from 'lodash';
5-
import normalizeStyle from './StyleNormalizer/normalizeStyle';
4+
import _ from 'lodash';
65

7-
import Theme, { ThemeShape } from './Theme';
6+
import normalizeStyle from './StyleNormalizer/normalizeStyle';
87
import { resolveComponentStyle } from './resolveComponentStyle';
8+
import Theme, { ThemeShape } from './Theme';
99

1010
// TODO - remove withRef warning in next version
1111

@@ -46,7 +46,12 @@ function getTheme(context) {
4646
* @returns {StyledComponent} The new component that will handle
4747
* the styling of the wrapped component.
4848
*/
49-
export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, options = {}) => {
49+
export default function connectStyle(
50+
componentStyleName,
51+
componentStyle = {},
52+
mapPropsToStyleNames,
53+
options = {}
54+
) {
5055
function getComponentDisplayName(WrappedComponent) {
5156
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
5257
}
@@ -75,7 +80,7 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
7580
);
7681
}
7782

78-
class StyledComponent extends React.PureComponent {
83+
class StyledComponent extends PureComponent {
7984
static contextTypes = {
8085
theme: ThemeShape,
8186
// The style inherited from the parent
@@ -112,8 +117,10 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
112117

113118
constructor(props, context) {
114119
super(props, context);
120+
115121
const styleNames = this.resolveStyleNames(props);
116122
const resolvedStyle = this.resolveStyle(context, props, styleNames);
123+
117124
this.setWrappedInstance = this.setWrappedInstance.bind(this);
118125
this.transformProps = this.transformProps.bind(this);
119126

@@ -137,10 +144,11 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
137144
};
138145
}
139146

140-
componentWillReceiveProps(nextProps, nextContext) {
141-
const styleNames = this.resolveStyleNames(nextProps);
142-
if (this.shouldRebuildStyle(nextProps, nextContext, styleNames)) {
143-
const resolvedStyle = this.resolveStyle(nextContext, nextProps, styleNames);
147+
componentDidUpdate(prevProps) {
148+
const styleNames = this.resolveStyleNames(this.props);
149+
150+
if (this.shouldRebuildStyle(prevProps, styleNames)) {
151+
const resolvedStyle = this.resolveStyle(this.context, this.props, styleNames);
144152

145153
this.setState({
146154
style: resolvedStyle.componentStyle,
@@ -152,7 +160,7 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
152160

153161
setNativeProps(nativeProps) {
154162
if (!this.isRefDefined()) {
155-
console.warn('setNativeProps can\'nt be used on stateless components');
163+
console.warn('setNativeProps can\'t be used on stateless components');
156164
return;
157165
}
158166
if (this.wrappedInstance.setNativeProps) {
@@ -164,19 +172,17 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
164172
this.wrappedInstance = component;
165173
}
166174

167-
hasStyleNameChanged(nextProps, styleNames) {
168-
return mapPropsToStyleNames && this.props !== nextProps &&
169-
// Even though props did change here,
170-
// it doesn't necessary means changed props are those which affect styleName
175+
hasStyleNameChanged(prevProps, styleNames) {
176+
return mapPropsToStyleNames && this.props !== prevProps &&
177+
// Even though props did change here, it doesn't necessarily mean
178+
// props that affect styleName have changed
171179
!_.isEqual(this.state.styleNames, styleNames);
172180
}
173181

174-
shouldRebuildStyle(nextProps, nextContext, styleNames) {
175-
return (nextProps.style !== this.props.style) ||
176-
(nextProps.styleName !== this.props.styleName) ||
177-
(nextContext.theme !== this.context.theme) ||
178-
(nextContext.parentStyle !== this.context.parentStyle) ||
179-
(this.hasStyleNameChanged(nextProps, styleNames));
182+
shouldRebuildStyle(prevProps, styleNames) {
183+
return (prevProps.style !== this.props.style) ||
184+
(prevProps.styleName !== this.props.styleName) ||
185+
(this.hasStyleNameChanged(prevProps, styleNames));
180186
}
181187

182188
resolveStyleNames(props) {

0 commit comments

Comments
 (0)