Skip to content
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

Merged
2 commits merged into from
Apr 12, 2017
Merged

implement setState #19

2 commits merged into from
Apr 12, 2017

Conversation

ghost
Copy link

@ghost ghost commented Apr 12, 2017

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

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) {
Copy link
Contributor

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)
Copy link
Contributor

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?

Copy link
Author

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

Copy link
Contributor

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', () => {
Copy link
Contributor

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)
Copy link
Contributor

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))
Copy link
Contributor

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

@bcherny
Copy link
Contributor

bcherny commented Apr 12, 2017

A few more behaviors, we can probably do in a separate PR/PRs:

  • Callback API for setState (as you mentioned)
  • Prevent direct assignment to state (at compile time, and at runtime)
  • Implement forceUpdate()
  • Use setImmediate instead of setTimeout (if the browser supports it)

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.

Copy link
Contributor

@bcherny bcherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @khoomeister!

@ghost ghost mentioned this pull request Apr 12, 2017
@ghost ghost merged commit 507cdd4 into master Apr 12, 2017
@ghost ghost deleted the ck-15-setState branch April 12, 2017 21:02
@ghost ghost mentioned this pull request Apr 12, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants