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> ) } }
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.
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.
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.
render() The render() method is the only required method in a class component.
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 }
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>; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With