-
-
Notifications
You must be signed in to change notification settings - Fork 633
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
add $F as a force property to the component instance #1535
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,214 @@ | ||
import { Component, render, rerender } from 'inferno'; | ||
|
||
describe('forceUpdate', () => { | ||
let container; | ||
|
||
beforeEach(function () { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(function () { | ||
rerender(); // Flush pending stuff, if any | ||
render(null, container); | ||
document.body.removeChild(container); | ||
container.innerHTML = ''; | ||
}); | ||
|
||
// https://jsfiddle.net/pnwLh7au/ | ||
it('Should have new state in render when changed state during setState + forceUpdate inside lifecycle methods and render only once', () => { | ||
let updated = 0; | ||
|
||
class Parent extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Child extends Component { | ||
state = { | ||
foo: 'bar' | ||
}; | ||
|
||
componentDidMount() { | ||
this.setState({ | ||
foo: 'bar2' | ||
}); | ||
this.forceUpdate(); | ||
} | ||
|
||
componentDidUpdate() { | ||
updated++; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{this.state.foo} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(<Parent />, container); | ||
expect(container.firstChild.firstChild.innerHTML).toBe('bar'); | ||
|
||
rerender(); | ||
|
||
expect(container.firstChild.firstChild.innerHTML).toBe('bar2'); | ||
expect(updated).toBe(1); | ||
}); | ||
|
||
it('Should ignore shouldComponentUpdate when forceUpdate called like React does', () => { | ||
let updated = 0; | ||
|
||
class Parent extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Child extends Component { | ||
state = { | ||
foo: 'bar' | ||
}; | ||
|
||
shouldComponentUpdate(prevProps, prevState) { | ||
if (prevState.foo !== this.state.foo) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
componentDidMount() { | ||
this.forceUpdate(); | ||
} | ||
|
||
render() { | ||
updated++; | ||
return ( | ||
<div> | ||
{this.state.foo} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(<Parent />, container); | ||
expect(container.firstChild.firstChild.innerHTML).toBe('bar'); | ||
expect(updated).toBe(1); | ||
|
||
rerender(); | ||
|
||
expect(container.firstChild.firstChild.innerHTML).toBe('bar'); | ||
expect(updated).toBe(2); | ||
}); | ||
|
||
|
||
// As per React https://jsfiddle.net/pnwLh7au/ | ||
// React has a different flow when setState is called outside lifecycle methods or event handlers (https://jsfiddle.net/egd1kuz6/), | ||
// but inferno has another flow for setState and Inferno. | ||
// Inferno collapses several `setState` even if they are called outside event listeners or lifecycle methods. So forceUpdate follows it | ||
it('Should use the updated state when forceUpdate called like React does even if shouldComponentUpdate ignores it', () => { | ||
let updated = 0; | ||
|
||
class Parent extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Child extends Component { | ||
state = { | ||
foo: 'bar' | ||
}; | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
componentDidMount() { | ||
this.setState({foo: 'bar2'}) | ||
this.forceUpdate(); | ||
} | ||
|
||
render() { | ||
updated++; | ||
return ( | ||
<div> | ||
{this.state.foo} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(<Parent />, container); | ||
expect(container.firstChild.firstChild.innerHTML).toBe('bar'); | ||
expect(updated).toBe(1); | ||
|
||
rerender(); | ||
|
||
expect(container.firstChild.firstChild.innerHTML).toBe('bar2'); | ||
expect(updated).toBe(2); | ||
}); | ||
|
||
// As per React https://jsfiddle.net/pnwLh7au/ | ||
it('Should use the updated state when forceUpdate called before setState like React does even if shouldComponentUpdate ignores it', () => { | ||
let updated = 0; | ||
|
||
class Parent extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Child extends Component { | ||
state = { | ||
foo: 'bar' | ||
}; | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
componentDidMount() { | ||
this.forceUpdate(); | ||
this.setState({foo: 'bar2'}) | ||
} | ||
|
||
render() { | ||
updated++; | ||
return ( | ||
<div> | ||
{this.state.foo} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
render(<Parent />, container); | ||
expect(container.firstChild.firstChild.innerHTML).toBe('bar'); | ||
expect(updated).toBe(1); | ||
|
||
rerender(); | ||
|
||
expect(container.firstChild.firstChild.innerHTML).toBe('bar2'); | ||
expect(updated).toBe(2); | ||
}); | ||
}); |
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.
Why is this line needed?
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.
As
$F
stores the information whether changes should be forced or not, we assume that they are not forced by default unlessforceUpdate
is called. The default value of$F
is false.If
forceUpdate
is called we switch$F
to true, and wait before it will be time for our component to render. After the component updates, we want to return the default value for the component. So, it's why I'm using$F = false
.And finally, the component potentially could plan further changes via
setState
or evenforceUpdate
during its updating cycle. It means we want to set up all defaults before the component starts its updating cycle. So I cache value inconst force
.