Skip to content

Commit

Permalink
[HMR] Better handle react-hot-loader (#8897)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Oct 29, 2017
1 parent 1d5394f commit 23ab36e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/styles/withStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,24 @@ const withStyles = (
this.attach(this.theme);

// Rerender the component so the underlying component gets the theme update.
// By theme update we mean receiving and applying the new class names.
this.setState({}, () => {
this.detach(oldTheme);
});
});
}

componentWillReceiveProps() {
// react-hot-loader specific logic
if (this.stylesCreatorSaved === stylesCreator || process.env.NODE_ENV === 'production') {
return;
}

this.detach(this.theme);
this.stylesCreatorSaved = stylesCreator;
this.attach(this.theme);
}

componentWillUnmount() {
this.detach(this.theme);

Expand Down
21 changes: 21 additions & 0 deletions src/styles/withStyles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,25 @@ describe('withStyles', () => {
});
});
});

describe('HMR with same state', () => {
it('should take the new stylesCreator into account', () => {
const styles1 = { root: { padding: 1 } };
const StyledComponent1 = withStyles(styles1, { name: 'MuiTextField' })(Empty);
const wrapper = shallow(<StyledComponent1 />);

const styles2 = { root: { padding: 2 } };
const StyledComponent2 = withStyles(styles2, { name: 'MuiTextField' })(Empty);

// Simulate react-hot-loader behavior
wrapper.instance().componentWillReceiveProps = // $FlowExpectedError
StyledComponent2.prototype.componentWillReceiveProps;

const classes1 = wrapper.props().classes.root;
wrapper.setProps({});
const classes2 = wrapper.props().classes.root;

assert.notStrictEqual(classes1, classes2, 'should generate new classes');
});
});
});
1 change: 1 addition & 0 deletions src/transitions/Collapse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('<Collapse />', () => {
assert.strictEqual(child.childAt(0).is('div'), true, 'should be a div');
assert.strictEqual(
child
.childAt(0)
.childAt(0)
.children()
.type(),
Expand Down

0 comments on commit 23ab36e

Please sign in to comment.