Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance implications of defining render props function within render()?

Take the following example code:

class Something extends Component {
  render() {
   return (
      <SomeProvider
        render={providedProps => (
          <SomeChild {...providedProps}/>
        )}
      />
    )
  }
}

Every React render props article uses this pattern as an example, but it's generally bad practice to define functions inline. Is this an exception to that rule?

Is there any benefit to defining the function outside of render?

class Something extends Component {
  renderSomeChild = providedProps => (
    <SomeChild {...providedProps}/>
  )

  render() {
   return (
      <SomeProvider
        render={this.renderSomeChild}
      />
    )
  }
}
like image 619
Slbox Avatar asked Sep 22 '26 16:09

Slbox


1 Answers

My suspicion was correct that the renderProps function should not be re-declared within the render function - or else it will force re-renders. I confirmed this Why Did You Render?

like image 183
Slbox Avatar answered Sep 24 '26 04:09

Slbox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!