Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React check how many times a component re-renders

I'm working on something and a piece of my code is a bit slow, what I think happens is that the component re-render too many times for no reason. Is there any way to check how many times a component re-renders in react(or react-native for the matter)? What i've tried to do is put a console.log after the render method and count how many there are but i'm not sure if that would work. Thanks in advance!

like image 391
Chris D Avatar asked Feb 07 '19 14:02

Chris D


People also ask

How do you know how many times a component re-renders?

Using React DevTools to highlight what components rerendered To enable it, go to "Profiler" >> click the "Cog wheel" on the right side of the top bar >> "General" tab >> Check the "Highlight updates when components render." checkbox.

How do you check if component re-renders in React?

React DevTools lets you highlight renders under Components -> View Settings -> Highlight updates when components render. This will show you the virtual renders. If you want to see native re-renders, you can do so in the Chrome DevTools, under the three-dot menu on the right -> More tools -> Rendering -> Paint flashing.

How do you avoid multiple re-renders in React?

1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With this technique, developers can avoid unnecessary renderings and reduce the computational load in applications.


1 Answers

you can place console.count('counter') in your render function to check this. Here you'll find all the details about console.count link

like image 80
Sifat Haque Avatar answered Jan 04 '23 11:01

Sifat Haque