Why is my Local(child) Component re-rendering?
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I'm not sure if it's a ReactJS lack of understanding, but since I'm coding this on NextJS, I decided to post it here.I have a
local component inside of my main component and it re-renders every time the state from the main component changes. If the local component remains the same, nothing has changed from it, why does it re-render? How do I prevent this from re-rendering and only make it re-render if there's a change in the child component? I'm trying to structure my code, but came across this issue. This is a sample code:import { useState } from "react"
const LocalComponent = () => {
console.log("My comp")
return <div>Local Component</div>
}
export default function MainComponent() {
const [count, setCount] = useState(0)
return <>
<div onMouseEnter={() => setCount(e => e + 1)} style={{ background: "#423eee", padding: "10px", width: "fit-content" }}>Hover</div>
Main Component {count}
<LocalComponent></LocalComponent>
</>
}Answered by Transvaal lion
I accidentally placed the local component
inside the main component, so whenever there was a state change, the entire component would re-render. I placed the local component outside of the main component and it's working as I intended.11 Replies
Giant panda
So this is expected behavior in React and in 95% of the cases isn't an issue, not even performance-wise.
MainComponent state changes and thus the entire component needs to rerender.In order to fix this you have various options to prevent unnecessary rerenders, but it's very hard to give blanket solutions since it heavily depends on the specific use-case and the variables and components surrounding all of this.
And as I've mentioned, in most cases it really doesn't matter.
@Giant panda And as I've mentioned, in most cases it really doesn't matter.
Transvaal lionOP
Well, I'm putting a list of IMGs and SVGs in a list in the local component and just displaying them. I have an animation that whenever you hover over an element in the
Main Component, the child re-renders.I can try and do a small recording and display it here so that you can see what I'm explaining.
With the local component and without the local component.Just let me know
Giant panda
A recording is of barely any use. A reproducible concrete example of why rerendering causes problems is much more helpful.
Then it's easier to give concrete and actionable advice how to reduce rerenders, if necessary
@Giant panda A recording is of barely any use. A reproducible concrete example of *why* rerendering causes problems is much more helpful.
Transvaal lionOP
It's far too big for me to post the concrete example.
Transvaal lionOP
I'll try to reproduce it at a smaller scale, just give me a bit.
@Giant panda A recording is of barely any use. A reproducible concrete example of *why* rerendering causes problems is much more helpful.
Transvaal lionOP
I accidentally placed the local component
inside the main component, so whenever there was a state change, the entire component would re-render. I placed the local component outside of the main component and it's working as I intended.Answer