Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 841a1c8

Browse files
lucasecdbMatt Goo
authored andcommitted
fix(ripple): setState warning when unmounting the component (#112)
1 parent e0c8e6e commit 841a1c8

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/ripple/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const withRipple = (WrappedComponent) => {
99

1010
foundation_ = null;
1111

12+
isMounted_ = true;
13+
1214
state = {
1315
classList: new Set(),
1416
style: {},
@@ -23,6 +25,7 @@ const withRipple = (WrappedComponent) => {
2325

2426
componentWillUnmount() {
2527
if (this.foundation_) {
28+
this.isMounted_ = false;
2629
this.foundation_.destroy();
2730
}
2831
}
@@ -41,9 +44,17 @@ const withRipple = (WrappedComponent) => {
4144
isUnbounded: () => this.props.unbounded,
4245
isSurfaceActive: () => instance[MATCHES](':active'),
4346
isSurfaceDisabled: () => this.props.disabled,
44-
addClass: (className) =>
45-
this.setState({classList: this.state.classList.add(className)}),
47+
addClass: (className) => {
48+
if (!this.isMounted_) {
49+
return;
50+
}
51+
this.setState({classList: this.state.classList.add(className)});
52+
},
4653
removeClass: (className) => {
54+
if (!this.isMounted_) {
55+
return;
56+
}
57+
4758
const {classList} = this.state;
4859
classList.delete(className);
4960
this.setState({classList});
@@ -109,6 +120,10 @@ const withRipple = (WrappedComponent) => {
109120
}
110121

111122
updateCssVariable = (varName, value) => {
123+
if (!this.isMounted_) {
124+
return;
125+
}
126+
112127
const updatedStyle = Object.assign({}, this.state.style);
113128
updatedStyle[varName] = value;
114129
this.setState({style: updatedStyle});

test/unit/ripple/index.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,17 @@ test('#adapter.addClass adds a class to the root element', () => {
182182
.hasClass('test-class'));
183183
});
184184

185-
test('#adapter.removeClass adds a class to the root element', () => {
185+
test('#adapter.addClass does not add class if isMounted is false', () => {
186+
const wrapper = mount(<DivRipple />);
187+
wrapper.instance().isMounted_ = false;
188+
wrapper.instance().foundation_.adapter_.addClass('test-class');
189+
assert.isFalse(
190+
wrapper.update()
191+
.find('.ripple-test-component')
192+
.hasClass('test-class'));
193+
});
194+
195+
test('#adapter.removeClass removes a class to the root element', () => {
186196
const wrapper = mount(<DivRipple />);
187197
wrapper.instance().foundation_.adapter_.addClass('test-class');
188198

@@ -195,12 +205,33 @@ test('#adapter.removeClass adds a class to the root element', () => {
195205
.hasClass('test-class'));
196206
});
197207

208+
test('#adapter.removeClass removes a class to the root element', () => {
209+
const wrapper = mount(<DivRipple />);
210+
wrapper.instance().foundation_.adapter_.addClass('test-class');
211+
212+
wrapper.instance().isMounted_ = false;
213+
wrapper.update();
214+
wrapper.instance().foundation_.adapter_.removeClass('test-class');
215+
216+
assert.isTrue(
217+
wrapper.update()
218+
.find('.ripple-test-component')
219+
.hasClass('test-class'));
220+
});
221+
198222
test('#adapter.updateCssVariable updates style', () => {
199223
const wrapper = mount(<DivRipple />);
200224
wrapper.instance().foundation_.adapter_.updateCssVariable('color', 'blue');
201225
assert.equal(wrapper.state().style.color, 'blue');
202226
});
203227

228+
test('#adapter.updateCssVariable does not update style if isMounted_ is false', () => {
229+
const wrapper = mount(<DivRipple />);
230+
wrapper.instance().isMounted_ = false;
231+
wrapper.instance().foundation_.adapter_.updateCssVariable('color', 'blue');
232+
assert.notEqual(wrapper.state().style.color, 'blue');
233+
});
234+
204235
test('#adapter.registerDocumentInteractionHandler triggers handler on document scroll', () => {
205236
const wrapper = mount(<DivRipple />);
206237
const testHandler = td.func();

0 commit comments

Comments
 (0)