How can I prevent the component from re-rendering? (with MWE)


gbewothcc

How can I prevent a component from re-rendering without changing its props ?

Code sandbox link/minimal working example . Move the cursor over Canvasand you can see that the console logs a number of "Canvas re-rendered"messages.

At the top level, I pass a constfunction as a handler Canvas. The onMouseMoveHandlerupdate Appso that it can update the state Detailsconsists of:

<div className="App">
  <Canvas onMouseMoveHandler={onMouseMoveHandler} />
  <Details passedProp={getValue} />
</div>

I need it so that it Canvasdoesn't re-render because the HTML canvaselements in it have user drawn lines. How can I do this?

I would have thought that using React.memowould make it act like a pure component, so it wouldn't be re-rendered.

I'm a beginner in React, if I'm not thinking about the correct code structure, please tell me how to fix it.

ki

Every time App.js is called setValue, the Appcomponent is re-rendered and a new onMouseMoveHandlerfunction is created. Your Canvascomponent uses, React.memobut since onMouseMoveHandlerit gets a new reference every time it re-renders, it also re-renders. To prevent this, you must onMouseMoveHandleruse useCallback. useCallback will return a memoized version of the callback, only changed if one of the dependencies has changed. .

Here is an example:

const onMouseMoveHandler = useCallback(() => {
   setValue(Math.random());
}, []);

Related


How can I prevent the component from re-rendering? (with MWE)

gbewothcc How can I prevent a component from re-rendering without changing its props ? Code sandbox link/minimal working example . Move the cursor over Canvasand you can see that the console logs a number of "Canvas re-rendered"messages. At the top level, I pa

How do I prevent component re-rendering on browser resizing?

mrtyvz61 In main layout component there are two different components that is MobileHeaderand DesktopHeaderwhen resize the browser it rerender these components any resize action. How can I stop rendering repeatedly when browser is resized? function useWindowSiz

How to prevent React from re-rendering the entire component?

center: I modified the following component definition from here as shown below. However, unlike the example, the component re-renders every time the mouse is moved over it. Re-rendering is pretty obvious: Does anyone know why this is happening? import React, {

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent this endless re-rendering reaction?

salon I'm building a React app, but I haven't quite figured out how to use hooks. My data is stored in Firestore in the following format: userlists > key1 > users: [user1, user2, user3] > date_created: date > listname: "Work C

How can I prevent CSS rendering from blocking my site?

Fire Phoenix I'm trying to optimize the loading speed of a mobile web page, for this I'm using this site: https://developers.google.com/speed/pagespeed/insights The site evaluated my resources and told me where I could improve. On my website I downloaded a fon