Use isMounted instead of instance map to check if mounted
Andrew Clark committed
Feb 7, 2017 at 13:36 UTC
3955e222d20fa95fc829a5fb9040ae467edc0fb8
3 files changed
+15
-14
scripts/fiber/tests-failing.txt
-3
@@ -1,9 +1,6 @@
1
src/isomorphic/classic/__tests__/ReactContextValidator-test.js
2
* should pass previous context to lifecycles
3
4
-src/renderers/__tests__/ReactComponentLifeCycle-test.js
5
-* should carry through each of the phases of setup
6
-
4
src/renderers/__tests__/ReactComponentTreeHook-test.js
5
* can be retrieved by ID
6
scripts/fiber/tests-passing.txt
+1
@@ -551,6 +551,7 @@ src/renderers/__tests__/ReactComponentLifeCycle-test.js
551
* should correctly determine if a null component is mounted
552
* isMounted should return false when unmounted
553
* warns if findDOMNode is used inside render
554
+* should carry through each of the phases of setup
555
* should not throw when updating an auxiliary component
556
* should allow state updates in componentDidMount
557
* should call nested lifecycle methods in the right order
src/renderers/__tests__/ReactComponentLifeCycle-test.js
+14
-11
@@ -13,7 +13,6 @@
13
14
var React;
15
var ReactDOM;
16
-var ReactInstanceMap;
16
var ReactTestUtils;
17
18
var clone = function(o) {
@@ -78,12 +77,9 @@ type ComponentLifeCycle =
77
'UNMOUNTED';
78
79
function getLifeCycleState(instance): ComponentLifeCycle {
81
- var internalInstance = ReactInstanceMap.get(instance);
82
- // Once a component gets mounted, it has an internal instance, once it
83
- // gets unmounted, it loses that internal instance.
84
- return internalInstance ?
85
- 'MOUNTED' :
86
- 'UNMOUNTED';
80
+ return instance.updater.isMounted(instance) ?
81
+ 'MOUNTED' :
82
+ 'UNMOUNTED';
83
}
84
85
/**
@@ -99,7 +95,6 @@ describe('ReactComponentLifeCycle', () => {
95
React = require('React');
96
ReactDOM = require('ReactDOM');
97
ReactTestUtils = require('ReactTestUtils');
102
- ReactInstanceMap = require('ReactInstanceMap');
98
});
99
100
it('should not reuse an instance when it has been unmounted', () => {
@@ -336,6 +331,9 @@ describe('ReactComponentLifeCycle', () => {
331
});
332
333
it('should carry through each of the phases of setup', () => {
334
+ spyOn(console, 'error');
335
+
336
+
337
class LifeCycleComponent extends React.Component {
338
constructor(props, context) {
339
super(props, context);
@@ -410,7 +408,7 @@ describe('ReactComponentLifeCycle', () => {
408
instance._testJournal.returnedFromGetInitialState
409
);
410
expect(instance._testJournal.lifeCycleAtStartOfWillMount)
413
- .toBe('MOUNTED');
411
+ .toBe('UNMOUNTED');
412
413
// componentDidMount
414
expect(instance._testJournal.stateAtStartOfDidMount)
@@ -419,11 +417,11 @@ describe('ReactComponentLifeCycle', () => {
417
'MOUNTED'
418
);
419
422
- // render
420
+ // initial render
421
expect(instance._testJournal.stateInInitialRender)
422
.toEqual(INIT_RENDER_STATE);
423
expect(instance._testJournal.lifeCycleInInitialRender).toBe(
426
- 'MOUNTED'
424
+ 'UNMOUNTED'
425
);
426
427
expect(getLifeCycleState(instance)).toBe('MOUNTED');
@@ -452,6 +450,11 @@ describe('ReactComponentLifeCycle', () => {
450
// But the current lifecycle of the component is unmounted.
451
expect(getLifeCycleState(instance)).toBe('UNMOUNTED');
452
expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);
453
+
454
+ expectDev(console.error.calls.count()).toBe(1);
455
+ expectDev(console.error.calls.argsFor(0)[0]).toContain(
456
+ 'LifeCycleComponent is accessing isMounted inside its render() function'
457
+ );
458
});
459
460
it('should not throw when updating an auxiliary component', () => {