Use isMounted instead of instance map to check if mounted

This commit is contained in:
Andrew Clark
2017-02-08 10:33:00 -08:00
parent 76a168fed6
commit 3955e222d2
3 changed files with 15 additions and 14 deletions
-3
View File
@@ -1,9 +1,6 @@
src/isomorphic/classic/__tests__/ReactContextValidator-test.js
* should pass previous context to lifecycles
src/renderers/__tests__/ReactComponentLifeCycle-test.js
* should carry through each of the phases of setup
src/renderers/__tests__/ReactComponentTreeHook-test.js
* can be retrieved by ID
+1
View File
@@ -551,6 +551,7 @@ src/renderers/__tests__/ReactComponentLifeCycle-test.js
* should correctly determine if a null component is mounted
* isMounted should return false when unmounted
* warns if findDOMNode is used inside render
* should carry through each of the phases of setup
* should not throw when updating an auxiliary component
* should allow state updates in componentDidMount
* should call nested lifecycle methods in the right order
@@ -13,7 +13,6 @@
var React;
var ReactDOM;
var ReactInstanceMap;
var ReactTestUtils;
var clone = function(o) {
@@ -78,12 +77,9 @@ type ComponentLifeCycle =
'UNMOUNTED';
function getLifeCycleState(instance): ComponentLifeCycle {
var internalInstance = ReactInstanceMap.get(instance);
// Once a component gets mounted, it has an internal instance, once it
// gets unmounted, it loses that internal instance.
return internalInstance ?
'MOUNTED' :
'UNMOUNTED';
return instance.updater.isMounted(instance) ?
'MOUNTED' :
'UNMOUNTED';
}
/**
@@ -99,7 +95,6 @@ describe('ReactComponentLifeCycle', () => {
React = require('React');
ReactDOM = require('ReactDOM');
ReactTestUtils = require('ReactTestUtils');
ReactInstanceMap = require('ReactInstanceMap');
});
it('should not reuse an instance when it has been unmounted', () => {
@@ -336,6 +331,9 @@ describe('ReactComponentLifeCycle', () => {
});
it('should carry through each of the phases of setup', () => {
spyOn(console, 'error');
class LifeCycleComponent extends React.Component {
constructor(props, context) {
super(props, context);
@@ -410,7 +408,7 @@ describe('ReactComponentLifeCycle', () => {
instance._testJournal.returnedFromGetInitialState
);
expect(instance._testJournal.lifeCycleAtStartOfWillMount)
.toBe('MOUNTED');
.toBe('UNMOUNTED');
// componentDidMount
expect(instance._testJournal.stateAtStartOfDidMount)
@@ -419,11 +417,11 @@ describe('ReactComponentLifeCycle', () => {
'MOUNTED'
);
// render
// initial render
expect(instance._testJournal.stateInInitialRender)
.toEqual(INIT_RENDER_STATE);
expect(instance._testJournal.lifeCycleInInitialRender).toBe(
'MOUNTED'
'UNMOUNTED'
);
expect(getLifeCycleState(instance)).toBe('MOUNTED');
@@ -452,6 +450,11 @@ describe('ReactComponentLifeCycle', () => {
// But the current lifecycle of the component is unmounted.
expect(getLifeCycleState(instance)).toBe('UNMOUNTED');
expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);
expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'LifeCycleComponent is accessing isMounted inside its render() function'
);
});
it('should not throw when updating an auxiliary component', () => {