Fix defaultProps/props theme theming#253
Fix defaultProps/props theme theming#253mxstbr merged 11 commits intostyled-components:masterfrom michalkvasnicak:fix/default-props-rerender
Conversation
Generated by 🚫 danger |
mxstbr
left a comment
There was a problem hiding this comment.
Amazing! Apart from the bot comments (add a CHANGELOG entry) I left a few comments inline, but other than those nitpicks this looks great! Thanks!
src/models/StyledComponent.js
Outdated
| } | ||
|
|
||
| // compare if there is any change | ||
| if (JSON.stringify(nextProps.theme) === JSON.stringify(this.state.theme)) { |
There was a problem hiding this comment.
Should this be a deep equality check instead of JSON.stringify? What's the difference?
There was a problem hiding this comment.
@mxstbr I didn't want to install new dependency just because of this. But I am wondering if deep equality check is necessary. What do you think?
Isn't === enough?
There was a problem hiding this comment.
Since the main difference between a deep equality check and the JSON.stringify method (which I find very smart btw, have never heard of that before) is that the deep equality check doesn't care about the ordering of the elements. (see here for more info)
How big is the a dependency that can do deep equality?
There was a problem hiding this comment.
@mxstbr I don't know if there is need to do other equality check than ===. Because if we pass the same theme to generateAndInjectStyles() we should get the same result right?
I am looking on this https://github.com/substack/node-deep-equal.
There was a problem hiding this comment.
Yeah I guess you're right. Let's bail out earlier if it's the exact same object, but otherwise do a stringify comparison:
if (nextProps.theme === this.state.theme || JSON.stringify(nextProps.theme) === JSON.stringify(this.state.theme)) {This way we don't stringify if it's exact same object, which saves quite a bit of time, but then just stringify and compare deeply if they aren't.
There was a problem hiding this comment.
Yeah this solution is better than introducing new dependency :)
src/models/StyledComponent.js
Outdated
| return | ||
| } | ||
|
|
||
| const { theme } = nextProps |
There was a problem hiding this comment.
Let's move this destructure to the function argument and use the theme variable throughout componentWillReceiveProps({ theme }).
There was a problem hiding this comment.
Done. I hope flowtype is correct.
src/test/theme.test.js
Outdated
| import jsdom from 'mocha-jsdom'; | ||
| import React from 'react' | ||
| import { render } from 'enzyme' | ||
| import { mount as render } from 'enzyme' |
There was a problem hiding this comment.
Why do we rename this to render? That's a separate mode, let's rename all instances of render to mount instead, otherwise this could be confusing.
There was a problem hiding this comment.
Yeah I forgot to refactor this :)
src/models/StyledComponent.js
Outdated
| nextProps | ||
| ) | ||
| this.setState({ generatedClassName }) | ||
| componentWillReceiveProps({ theme }: { theme: ?{ [key: string]: mixed } }) { |
There was a problem hiding this comment.
I think this should be ({ theme }: { theme?: Object })? I'm not a flow type expert though, so maybe I'm wrong. @styled-components/typers how do we do this?
There was a problem hiding this comment.
@mxstbr This annotation is the same as Theme in ThemeProvider. I can export Theme type from ThemeProvider component and use it here. Then it would be { theme: ?Theme }.
There was a problem hiding this comment.
@mxstbr Theme type is now exported from ThemeProvider and used in StyledComponent and StyleNativeComponent. What do you think?
|
Oh sorry, I just realized that this will probably increase our rendering time since we're now doing a full Sorry for the troubles, thanks! |
|
@mxstbr done 😉 |
…ps and re-render caused by props.theme update
…heme updated directly in props
…nt when theme is changed in props
|
@mxstbr Do not merge this yet, there is bug in |
|
@mxstbr ok fixed |
|
Amazing work, thanks so much! Shipping a new version with these changes very soon! |
Fixes #252
Fixes #247