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

Warn if calling setState in getChildContext #6114

Closed
blainekasten opened this issue Feb 24, 2016 · 12 comments
Closed

Warn if calling setState in getChildContext #6114

blainekasten opened this issue Feb 24, 2016 · 12 comments

Comments

@blainekasten
Copy link
Contributor

This code causes a maximum call stack:

getChildContext() {
  this.setState({
    foo: 'bar',
  });

  return {
    foo: this.state.foo,
  };
}

These sort of errors are hard to track down as the stack trace is deep in react. Is this a place we can give a user an invariant to let them know what they did wrong?

@jimfb
Copy link
Contributor

jimfb commented Feb 24, 2016

We should warn if you call setState in getChildContext.

@jimfb jimfb changed the title Maximum call stack from setState() in getChildContext Warn if calling setState in getChildContext Feb 24, 2016
@raineroviir
Copy link

I'll do it!

@raineroviir
Copy link

Reproduced error (in the console)

@raineroviir
Copy link

What's a good way to start on this?

@jimfb
Copy link
Contributor

jimfb commented Feb 25, 2016

@raineroviir Find the places that getChildContext is called, and the places that setState is defined, and warn if setState is called during getChildContext. I think we already do basically the same thing for calling setState within render, so you can use that as a thing to copy.

@raineroviir
Copy link

Ok, thank you!

@raineroviir
Copy link

Can you give me some more pointers.. I'm stuck figuring out whether setState is called during getChildContext.
I've tried Function.caller as in ReactComponent.prototype.setState.caller but got a type error. And trying to access the context with this hasn't revealed any clues either.

Here's the warning code shown when trying to setState within render which shows a pretty simple predicate for the warning, I'm just curious if I should try to do that as well?:

ReactCurrentOwner.current == null,
'%s(...): Cannot update during an existing state transition (such as ' +
'within `render` or another component\'s constructor). Render methods ' +

@jimfb
Copy link
Contributor

jimfb commented Feb 25, 2016

getChildContext might not be the immediate caller, so that won't work. You're going to need to set some state before and after getChildContext. We already warn when users call setState in render, take a look at that code.

@dan-weaver
Copy link

should ReactCurrentOwner.current be set during a getChildContext call? Looks like it currently is not.

as seen here

and

here

@jimfb
Copy link
Contributor

jimfb commented Feb 25, 2016

@dan-weaver No, I think we only set the owner in render.

@gaearon
Copy link
Collaborator

gaearon commented Feb 28, 2016

FYI this is being worked on in #6121.

@jimfb jimfb closed this as completed in 9c1916d Mar 5, 2016
jimfb added a commit that referenced this issue Mar 5, 2016
Fix #6114 - Calling setState inside getChildContext should warn
@SunflowerPKU
Copy link

Ok, thank you!

Hi @raineroviir

I am a Ph.D. student. I am doing research about helping newcomers participate in OSS projects. I noticed that many projects are using labels such as 'good first issue/bug, difficulty/newcomer' for issues to recommend that newcomers start from these tasks.

I noticed that you tried to solve this issue, but unfortunately, you had not contributed successfully. I also found there are many newcomers feel difficult when submitting their first pr. Therefore, I want to optimize this mechanism, which needs your help. I have some questions and wish your valuable feedback.

  1. What problems did you meet when solving this issue?
  2. Do you think it is appropriate to recommend newcomers to solve this issue? and why?
  3. How do you think of the existing mechanism that adding certain labels to issues for helping newcomers participate in open source projects?

I am looking forward to hearing from you soon. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants