mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Use isMounted instead of instance map to check if mounted
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user