Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React functions inside render()

Is there a preference on where you put functions inside a react component. I am still learning react so just trying to figure out the best practices.

class Content extends React.Component {     /// Whats the difference between putting functions here such as     Hello(){     }    render(){       /// or here    Hello(){     }       return()(       <div>blah blah </div>      )      }     } 
like image 306
Whymess Avatar asked Dec 28 '16 20:12

Whymess


People also ask

Can I write a function in render React?

Using JSX, you can create a function and return a set of JSX elements to a variable, and that variable used is to render the elements inside the render() function in React. This guide will help you learn how to return JSX to a variable and render JSX markups to the DOM.

Can we call function inside render?

In this article, we will learn how to call a function to render in ReactJS. React is having 2 types of components, i.e, Class based and Functional Components, but render method is only in Class based components. So we will make a class based component and call a function in that.

How do you use render function in React?

React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.

Is it mandatory for each React component to have a render () function?

render() The render() method is the only required method in a class component.


2 Answers

A function in the render method will be created each render which is a slight performance hit. It's also messy if you put them in the render, which is a much bigger reason, you shouldn't have to scroll through code in render to see the html output. Always put them on the class instead.

For stateless components, it's probably best to keep functions outside of the main function and pass in props instead, otherwise the function will be created each render too. I haven't tested performance so I don't know if this is a micro-optimization but it's worth noting.

Example:

const MyStatelessComponent = ({randomProp}) => (     render() {         doSomething(randomProp);          return <div />     } );  doSomething = (randomProp) => {     //Do something here } 
like image 106
Martin Dawson Avatar answered Sep 23 '22 06:09

Martin Dawson


It's worth pointing out that there are times when you want to perform intensive calculations in the render() and take the performance hit. Especially when it involves making calculations from props. Take the case of

class Person extends React.Component {   constructor(props) {     super(props);     this.state = {       name: props.firstName + props.lastName,     };   }    render() {     return <div> {this.state.name} </div>;   } } 

Now when props changes, the state won't be updated as the constructor function only runs when the component is mounted. A better way would be to make the calculation in render. So whenever your component rerenders, it recalculates and renders the right value.

class Person extends React.Component {   render() {     const myName = this.props.firstName + this.props.lastName;      return <div> {myName} </div>;   } } 

And this version is a bit cleaner to read:

class Person extends React.Component {   calculateName = () => {     return this.props.firstName + this.props.lastName;   }    render() {     const myName = this.calculateName();      return <div> {myName} </div>;   } } 
like image 35
Gunther Avatar answered Sep 24 '22 06:09

Gunther