-
Notifications
You must be signed in to change notification settings - Fork 16
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
implement setState #19
Conversation
index.ts
Outdated
|
||
// note: this isn't meant to exactly replicate React's setState (e.g. http://stackoverflow.com/q/28922275/7816712) | ||
protected setState(newState: State) { |
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.
Partial<State>
index.ts
Outdated
// note: this isn't meant to exactly replicate React's setState (e.g. http://stackoverflow.com/q/28922275/7816712) | ||
protected setState(newState: State) { | ||
if (this.__setStateTriggersLifecycle) { | ||
setTimeout(() => this.lifecycle({} as Props, newState), 0) |
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.
Is this how React does it?
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.
set comment above - it's sometimes sync or async... haven't looked into exactly how this behavior works
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.
Looking at the SO post you linked, it seems to be always sync, no?
@@ -258,6 +258,98 @@ describe('Component', () => { | |||
}) | |||
}) | |||
|
|||
describe('#setState', () => { |
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.
Can you add test cases for triggering setState in constructor, render(), shouldComponentUpdate(), componentWillUnmount()
index.ts
Outdated
const shouldUpdate = this.shouldComponentUpdate(nextProps, this.state) | ||
assign(this, { props: nextProps }) | ||
this.avoidSetStateLifecycle(() => this.componentWillReceiveProps(nextProps)) | ||
const shouldUpdate = this.shouldComponentUpdate(nextProps, nextState) |
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.
Can you add test for nextState in shouldComponentUpdate
this.componentWillReceiveProps(nextProps) | ||
const shouldUpdate = this.shouldComponentUpdate(nextProps, this.state) | ||
assign(this, { props: nextProps }) | ||
this.avoidSetStateLifecycle(() => this.componentWillReceiveProps(nextProps)) |
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.
Can you add a test that this.state
still refers to the old state inside componentWillReceiveProps
A few more behaviors, we can probably do in a separate PR/PRs:
Another thought: we can deviate from React here, and allow direct assignment to this.state (rather than force people to use setState) while still triggering appropriate lifecycle hooks if we use an ES6 proxy. |
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.
Awesome work @khoomeister!
Connects to #15
OK this doesn't implement everything in setState yet (e.g. doesn't do the callback param yet) but this is a start which should be good for most use cases.
setState doc: https://facebook.github.io/react/docs/react-component.html#setstate