React components re-render on their own whenever there are some changes in their props or state. Simply updating the state, from a random place in the code, causes the User Interface (UI) elements that get re-rendered automatically.
In class components, you have the option to call force update to force a rerender. In function components, however, there’s no chance of force update as there is no equivalent, but you have the option to contrive a way to force updates with the help of the useState hook. Force update must be tried and avoided as it deviates from a React mindset. The React docs cite some examples of when force updates can be used.
By default, when there is a change in a component’s state or props, the component will re-render. However, if there are implicit changes like changes in deep data within an object that too without the object itself changing or if your method to render depends on another data, you have the option to tell React that it requires to re-run render just by calling force update.
Force Update:
However, an idea has been proposed that with deeply nested objects, what becomes necessary is the force update. With taking help of an immutable data source, it becomes cheap to track changes. The change will always lead to a new object. Thus, we are only required to check and see if the object reference has changed or not. You can even use the library Immutable JS to implement immutable data objects into the app.
Generally, you must try to avoid the use of force updates and should only read from this. props as well as this. state that is there in the render. This makes the react component “pure” and the application much easier and at the same time quite efficient. Changing the element key that you want to re-render will work. You must set the key prop on the element via state and then set the state to have a new key when you want to update.
By doing this, a change occurs and then you are required to reset the key to this. setState ({key: Math. random}); You must note that this will help you replace the element that is changing the key. An example of how it could be useful is when there is a file input field that you want to reset after uploading an image.
Review your Code:
Also, you must note that if you are using force updates, you may want to review your code and check if there is any other way to do the same thing. Changing the key) will replace the element completely. If you update the key to bring the required changes, you will probably face an issue somewhere or the other in your code. Thus, using Math. random the n key can help you re-create the element with every render. It is not recommended to update the key in this manner because react uses the key to effectively determine the best possible way to re-render things.
React developers struggle with unnecessary re-render components in their applications. We have all gone through this when one component keeps updating its data in the background and thus the overall performance changes.
A quick note on Render:
React’s createElement function help in creating and returning a new element as per the given element type. All the updates are automatically done whenever it’s required. Let us now see how the re-render works in the class as well as the functional components.
Here are a few methods to re-render a React component.
Re-render component when there is a change in state:
Whenever a React component state changes, React must run the render method.
import React from 'react'
export default class App extends React.Component {
componentDidMount() {
this.setState({});
}
render() {
console.log('render() method')
return <p>Hi!<p>;
}
}
In the example mentioned above, the state when the component mounts are updated.
You even have the option to re-render an event component, for example, a click event.
import React from "react";
export default class App extends React.Component {
state = {
mssg: ""
};
handleClick = () => {
this.setState({ mssg: "Hi there!" });
};
render() {
console.log("render() method");
return (
<>
{this.state.mssg}
</> ); } }
Output:
Say something
Both the outputs will look somewhat like this:
render() method
render() method
Re-render component when props change:
import React from 'react'
class Child extends React.Component {
render() {
console.log('Child component: render()');
return
}
}
In the example above, component does not have a state. However, it has a custom prop that is the message that it accepts.
When the button is clicked on, it will update the component, and the render lifecycle will be made to run again.
Child component: render()
Child component: render()
Re-render with a key prop:
You can change the value of the key prop, and it will make React unmount and re-mount the component again, and go through the render lifecycle.
Force a re-render:
This method is highly discouraged and not a recommended one. You should always use props & state to make a new render.
Nevertheless, this is how you may do it.
import React from 'react'
export default class App extends React. Component {
handleClick = () => {
// force a re-render
this.forceUpdate();
};
render() {
console.log('App component: render()')
return (
<>
</>
);
}
}
Output:
Say something
Conclusion:
You must try to make your re-render count using the above-mentioned resources that include in its talk the different examples and case scenarios. If you are required to re-render a React component, you should update the components state and props Always.
Try avoiding and causing re-render with a key prop as it will make it more complex. Although there are some odd use cases where it is required. However, you must keep in mind to never use force updates to cause a re-render.
Top comments (0)