admin管理员组

文章数量:1320888

When does ReRendering exactly happen in React. At the end of the method or right away ?

// This is pseudo React Code
class myComponent {

@State
String x;

someFunction() {
  x= x+" World"; // line 1
  .
  .
  // a lot of code in this function 
  // line 149
}

}

So when will rerender happen. On Line 1 or after Line 149 ??

When does ReRendering exactly happen in React. At the end of the method or right away ?

// This is pseudo React Code
class myComponent {

@State
String x;

someFunction() {
  x= x+" World"; // line 1
  .
  .
  // a lot of code in this function 
  // line 149
}

}

So when will rerender happen. On Line 1 or after Line 149 ??

Share Improve this question edited Jan 29 at 12:25 Peter_19 asked Jan 29 at 10:28 Peter_19Peter_19 113 bronze badges 4
  • Please tag your question accordingly. @State isn't part of vanilla react, you must be using something else to enable that decorator – Nick Parsons Commented Jan 29 at 11:26
  • this seems more like stenciljs, not reactjs – Nick Parsons Commented Jan 29 at 11:34
  • it's just pseudo Code. I just want to know how it works in React. – Peter_19 Commented Jan 29 at 12:25
  • Neither. Not after line 1 and not after line 149. Since your component looks like the old-style class component, it first calls setState in your function and then, when your function ends, pending changes can trigger rendering. – Wiktor Zychla Commented Jan 29 at 12:31
Add a comment  | 

1 Answer 1

Reset to default 0

React does not re-render immediately. Instead, it waits until the function completes and then processes state updates in batches.

本文标签: reactjsWhen is Rerendering in React triggeredStack Overflow