Skip to content

Commit

Permalink
fix for podefr#5
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhm committed Apr 2, 2018
1 parent 2ae773c commit a03bea4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
32 changes: 32 additions & 0 deletions e2e/src/unmount.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

import App from './App';

enzyme.configure({ adapter: new Adapter() });

describe('Given App is rendered and unmounted before final debounced render', () => {
let wrapper;

beforeEach(() => {
jest.useFakeTimers();
wrapper = enzyme.mount(<App />);
});

it('does not throw an unmounted component error', async () => {
return new Promise((resolve) => {
// unmount after debounced execution is queued, but before it gets executed.
setTimeout(() => {
wrapper.unmount();
}, 95);

setTimeout(() => {
resolve()
}, 110);
jest.runAllTimers();
});
})
});


12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function debounceRender(ComponentToDebounce, ...debounceArgs) {
return class DebouncedContainer extends Component {
constructor(props) {
super(props);

this._isMounted = true;
this.state = props;
this.shouldRender = false;
}
Expand All @@ -15,13 +15,19 @@ export default function debounceRender(ComponentToDebounce, ...debounceArgs) {
this.updateState(props);
}

componentWillUnmount() {
this._isMounted = false;
}

updateState = debounce(props => {
this.shouldRender = true;
this.setState(props);
if (this._isMounted) {
this.setState(props);
}
}, ...debounceArgs);

shouldComponentUpdate() {
return this.shouldRender;
return this.shouldRender && this._isMounted;
}

render() {
Expand Down

0 comments on commit a03bea4

Please sign in to comment.