-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: replace PureComponent with shouldComponentUpdate #534
fix: replace PureComponent with shouldComponentUpdate #534
Conversation
Codecov Report
@@ Coverage Diff @@
## master #534 +/- ##
==========================================
+ Coverage 75.57% 75.63% +0.05%
==========================================
Files 181 181
Lines 5651 5684 +33
Branches 1102 1102
==========================================
+ Hits 4271 4299 +28
- Misses 1364 1369 +5
Partials 16 16
Continue to review full report at Codecov.
|
hi @patrykkopycinski thanks for this. I'm in the meantime of a refactoring that will move away from react for some of these components (the one used to render the chart). |
Sounds great @markov00! Do you know maybe when you will be able to merge your refactor? Because our app is heavily utilizing charts, so it has a big footprint on the performance |
I will push the PR today, it needs a review and then it will be merged |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrykkopycinski let's merge this. I will merge mine changes later.
Thanks for the contribution
@@ -39,6 +40,11 @@ export class AreaGeometries extends React.PureComponent<AreaGeometriesDataProps, | |||
overPoint: undefined, | |||
}; | |||
} | |||
|
|||
shouldComponentUpdate(nextProps: AreaGeometriesDataProps, nextState: AreaGeometriesDataState) { | |||
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can avoid checking the equality of the state. This is not used anymore in this component and should be removed (there is no relevant use of the overPoint
state object in the component)
@@ -31,6 +32,11 @@ export class BarGeometries extends React.PureComponent<BarGeometriesDataProps, B | |||
overBar: undefined, | |||
}; | |||
} | |||
|
|||
shouldComponentUpdate(nextProps: BarGeometriesDataProps, nextState: BarGeometriesDataState) { | |||
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, overBar is no more used (no one is setting it), you can remove the deepEqual for the state
@@ -37,6 +38,10 @@ export class LineGeometries extends React.PureComponent<LineGeometriesDataProps, | |||
}; | |||
} | |||
|
|||
shouldComponentUpdate(nextProps: LineGeometriesDataProps, nextState: LineGeometriesDataState) { | |||
return !deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Codecov Report
@@ Coverage Diff @@
## master #534 +/- ##
==========================================
+ Coverage 75.57% 75.80% +0.22%
==========================================
Files 181 193 +12
Lines 5651 5844 +193
Branches 1102 1116 +14
==========================================
+ Hits 4271 4430 +159
- Misses 1364 1397 +33
- Partials 16 17 +1
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #534 +/- ##
=========================================
+ Coverage 75.57% 75.8% +0.22%
=========================================
Files 181 193 +12
Lines 5651 5844 +193
Branches 1102 1116 +14
=========================================
+ Hits 4271 4430 +159
- Misses 1364 1397 +33
- Partials 16 17 +1
Continue to review full report at Codecov.
|
Thank you very much for help @markov00 💪 |
## [17.0.1](v17.0.0...v17.0.1) (2020-02-05) ### Bug Fixes * replace PureComponent with shouldComponentUpdate ([#534](#534)) ([5043725](5043725))
🎉 This PR is included in version 17.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This commit replaces React.PureComponent with React.Component with custom shouldComponentUpdate that deeply compares props. Also, it fixes shouldComponentUpdate logic in ChartContainerComponent that caused the component to re-render every time the props have changed.
This commit replaces React.PureComponent with React.Component with custom shouldComponentUpdate that deeply compares props. Also, it fixes shouldComponentUpdate logic in ChartContainerComponent that caused the component to re-render every time the props have changed. Co-authored-by: patrykkopycinski <contact@patrykkopycinski.com>
## [17.0.1](elastic/elastic-charts@v17.0.0...v17.0.1) (2020-02-05) ### Bug Fixes * replace PureComponent with shouldComponentUpdate ([opensearch-project#534](elastic/elastic-charts#534)) ([e06aeb2](elastic/elastic-charts@e06aeb2))
Summary
During working on elastic/kibana#56587 I have found out that charts-related components are probably re-renders unnecessarily, so I have decided to verify that and here is the outcome from https://github.com/welldone-software/why-did-you-render
Each line in the console means unnecessary component's re-render and after the row is expanded we can see which props are equal by value but not by reference, so it causes
PureComponent
to re-render.After my changes
Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.src/index.ts
(and stories only import from../src
except for test data & storybook)