-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: avoid throw error when update unmounted component #2223
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab05f3b
refactor: rework bundle core logic
SoloJiang 58a364f
chore: ci
SoloJiang 8709c0b
chore: revert
SoloJiang c82a034
chore: lint
SoloJiang 2935ebb
chore: ci
SoloJiang fced402
chore: format code
SoloJiang 2b7c922
chore: avoid throw error when update unmounted component
SoloJiang 85d7f2a
Merge branch 'release/1.2.1' into fix/node-destroyed
SoloJiang fab69e2
chore: remove useless comment
SoloJiang d86da1e
chore: preserve warning
SoloJiang 1730cde
chore: typo
SoloJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* @jsx createElement */ | ||
|
||
import createElement from '../createElement'; | ||
import Component from '../vdom/component'; | ||
import render from '../render'; | ||
import Host from '../vdom/host'; | ||
import ServerDriver from 'driver-server'; | ||
import { useState, useEffect } from '../hooks'; | ||
|
||
describe('update unmounted component', () => { | ||
function createNodeElement(tagName) { | ||
return { | ||
nodeType: 1, | ||
tagName: tagName.toUpperCase(), | ||
attributes: {}, | ||
style: {}, | ||
childNodes: [], | ||
parentNode: null | ||
}; | ||
} | ||
|
||
beforeEach(function() { | ||
Host.driver = ServerDriver; | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(function() { | ||
Host.driver = null; | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should warn about class component', () => { | ||
const container = createNodeElement('div'); | ||
let destroyChild; | ||
|
||
class Child extends Component { | ||
state = { | ||
name: 'hello' | ||
} | ||
componentDidMount() { | ||
setTimeout(() => { | ||
this.setState({ | ||
name: 'work' | ||
}); | ||
}, 1000); | ||
} | ||
render() { | ||
return <div>{this.state.name}</div>; | ||
} | ||
} | ||
|
||
class App extends Component { | ||
state = { | ||
showChild: true | ||
} | ||
componentDidMount() { | ||
destroyChild = () => { | ||
this.setState({ | ||
showChild: false | ||
}); | ||
}; | ||
} | ||
render() { | ||
return (<div> | ||
{ this.state.showChild ? <Child /> : null } | ||
</div>); | ||
} | ||
} | ||
|
||
expect(() => { | ||
render(<App />, container); | ||
destroyChild(); | ||
jest.runAllTimers(); | ||
}).toWarnDev("Warning: Can't perform a Rax state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.", { withoutStack: true }); | ||
}); | ||
|
||
it('should warn about function component', () => { | ||
const container = createNodeElement('div'); | ||
let destroyChild; | ||
|
||
function Child() { | ||
const [name, setName] = useState('hello'); | ||
useEffect(() => { | ||
setTimeout(() => { | ||
setName('world'); | ||
}, 1000); | ||
}, []); | ||
return <div>{name}</div>; | ||
} | ||
|
||
class App extends Component { | ||
state = { | ||
showChild: true | ||
} | ||
componentDidMount() { | ||
destroyChild = () => { | ||
this.setState({ | ||
showChild: false | ||
}); | ||
}; | ||
} | ||
render() { | ||
return (<div> | ||
{ this.state.showChild ? <Child /> : null } | ||
</div>); | ||
} | ||
} | ||
|
||
expect(() => { | ||
render(<App />, container); | ||
destroyChild(); | ||
jest.runAllTimers(); | ||
}).toWarnDev("Warning: Can't perform a Rax state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.", { withoutStack: true }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,18 @@ function requestUpdate(component, partialState, callback) { | |
let internal = component[INTERNAL]; | ||
|
||
if (!internal) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
// Block other render | ||
Host.__isUpdating = false; | ||
console.error( | ||
"Warning: Can't perform a Rax state update on an unmounted component. This " + | ||
'is a no-op, but it indicates a memory leak in your application. To ' + | ||
'fix, cancel all subscriptions and asynchronous tasks in %s.', | ||
component.__isReactiveComponent | ||
? 'a useEffect cleanup function' | ||
: 'the componentWillUnmount method', | ||
); | ||
} | ||
return; | ||
} | ||
|
||
|
@@ -117,6 +129,10 @@ function requestUpdate(component, partialState, callback) { | |
|
||
// setState | ||
if (partialState) { | ||
// Function Component should force update | ||
if (component.__isReactiveComponent) { | ||
internal.__isPendingForceUpdate = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段位置调整的原因是? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 将之前 ReactiveComponent |
||
} | ||
enqueueState(internal, partialState); | ||
// State pending when request update in componentWillMount and componentWillReceiveProps, | ||
// isPendingState default is false value (false or null) and set to true after componentWillReceiveProps, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是哪个组件调用的, warning 中能透出不
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
日志可能会很多