Skip to content
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

[ButtonBase] Better performance #11277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/material-ui/src/ButtonBase/ButtonBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import keycode from 'keycode';
import { assert } from 'chai';
import PropTypes from 'prop-types';
import { spy, useFakeTimers } from 'sinon';
import rerender from 'test/utils/rerender';
import { createShallow, createMount, getClasses, unwrap } from '../test-utils';
import TouchRipple from './TouchRipple';
import ButtonBase from './ButtonBase';
Expand Down Expand Up @@ -658,4 +659,22 @@ describe('<ButtonBase />', () => {
assert.strictEqual(wrapper.find('.focusVisible').length, 1);
});
});

describe('rerender', () => {
beforeEach(() => {
rerender.spy();
});

afterEach(() => {
rerender.reset();
});

it('should not rerender the TouchRipple', () => {
const wrapper = mount(<ButtonBase>foo</ButtonBase>);
wrapper.setProps({
children: 'bar',
});
assert.strictEqual(rerender.updates.length, 1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 3 without PureComponent.

});
});
});
2 changes: 1 addition & 1 deletion packages/material-ui/src/ButtonBase/TouchRipple.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const styles = theme => ({
},
});

class TouchRipple extends React.Component {
class TouchRipple extends React.PureComponent {
state = {
nextKey: 0,
ripples: [],
Expand Down
30 changes: 30 additions & 0 deletions test/utils/rerender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import getDisplayName from 'recompose/getDisplayName';

function createComponentDidUpdate(instance) {
return function componentDidUpdate() {
const displayName = getDisplayName(this);
instance.updates.push({
displayName,
});
};
}

// Inspired by https://github.com/maicki/why-did-you-update
class Rerender {
updates = [];

spy = () => {
this.savedCDU = React.Component.prototype.componentDidUpdate;
React.Component.prototype.componentDidUpdate = createComponentDidUpdate(this);
};

savedCDU = null;

reset = () => {
this.updates = [];
React.Component.prototype.componentDidUpdate = this.savedCDU;
};
}

export default new Rerender();