-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Calling setState in render causes infinite loop #5591
Comments
In general, this should never happen; you should not be doing anything remotely related to modifying any component's state within a render function. I'm curious what your use case is, for having a callback which does a setState; mind sharing? |
I ran into this by accident once... I had |
A video player is playing. I have an Component which sets styles on the scrub bar. When the Component that sets styles updates it fires a callback. (there's a good reason for this but it's beyond explaining in a simple issue) I was listening to this callback and calling In general |
If I'm understanding your flow correctly, I still don't see how/why a @mikkoh wrote:
I assume you're using |
This is an oversimplification of the code but:
When Again this is an oversimplification and what |
The fact that we support calling |
@mikkoh Did you find a workaround? |
I ended up implementing @spicyj |
@mikkoh Can you provide a specific code example here? I'm not sure I understand exactly what your issue was. |
@spicyj I posted a snippet earlier. So below when
|
I'm still failing to understand why what you're doing causes an infinite loop but that you say adding a If you can post a working code example (in jsbin, for example) that shows the problem, I'm happy to take another look – but as is since I can't understand what you're doing this is not very actionable for me. I don't understand what the intention of your code is if you're trying to update the parent's state whenever a child renders. Wouldn't you expect that to cause an infinite loop? |
Possibly related to this: facebook/react#5591
I realize this is an old ticket and there might be more idiomatic ways of doing things in React now - but I just hit an issue which is related, so figured here's as good a place as any to comment ;) What's the best way to handle situations where you need to measure after the component has been mounted? I'd prefer to avoid Here's an example of what I mean:
The issues I have with this approach are:
I'd love to hear thoughts on why this is or isn't the right way to do it. Thanks! |
|
So so late to the party here but @sophiebits's comment about componentDidMount has just brought an end to 3 hours of debugging - thank you! |
I've a strange issue. |
@spyx08 can we see a code sample? Here's what will cause an infinite loop, assuming you're not doing any state change checks:
Make sure you're not doing any of the above and you shouldn't see an infinite loop. |
I am also facing a similar issue as @mikkoh : where I'm passing down a parent function through props, to be called from a button inside a child component; which is going into an infinite loop. handleSomething(value) { render() { where I am only calling this.props.onCallback from a button inside my child. Help! |
@lisadesouza89, all depends on how exactly your are calling the this.prrops.onCallback inside the Button. Could you provide this information? |
Parent component:
Child1:
Child2:
|
You shouldn't call init within onClick declaration. Since you know mRecord within parent you could bind it to initModal inside parent's constructor.
|
There are multiple Child1 and Child2 elements with different values, so no, I don't know mRecord in the parent. |
how to use react native flex radio button inside for loop |
I have a simple solution, compares the state before you change, it worked for me. |
I have developed a picker for setting day and night. I getting the current time for a picker value from an API and trying to set with setState, but every time it will display 'day' as a value. I want it should display based on the value passed. How can I solve the issue here
|
|
I was just passing my lockdown times learning react. I came into a similar situation. So react does go into an infinite loop when I call setState directly inside the render, but react is smart enough to detect it and stops, throwing and error that something dumb has happend. But if I do a dynamic import and call setState inside then(), it keeps on rendering, and it got my CPU fans roaring... any reasons why it doesn't detect "Maximum update depth exceeded" when setState is called in such a way? |
This may seem really silly to do to call
setState
in render. However it's possible for this to happen if a component has a callback which is being called immediately during render and in your callback handler you callsetState
.The text was updated successfully, but these errors were encountered: