I've noticed an issue where every component within the same parent (App in below example) will rerender when unrelated states/props change, making the page/forms noticeable slower.
I've followed many advice like memoizing event handlers & props, but unrelated components still re-renders. I'm stumped. What am I not understanding about React?
[CodeSandbox] In React debugger, enable: Highlight updates when components render
import React, { useMemo, useState } from "react";
import { TextField } from "@material-ui/core";
function MyTextInput(props) {
return (
<TextField
variant={"outlined"}
onChange={props.onChange}
value={props.value}
/>
);
}
export default function App() {
const [exampleTextValue1, setExampleTextValue1] = useState("");
const [exampleTextValue2, setExampleTextValue2] = useState("");
const handleChange1 = useMemo(
() => (event) => setExampleTextValue1(event.target.value),
[]
);
const handleChange2 = useMemo(
() => (event) => setExampleTextValue2(event.target.value),
[]
);
return (
<>
<div>
Change me:
<MyTextInput value={exampleTextValue1} onChange={handleChange1} />
</div>
<div>
Unrelated inputs. Should not re-render:
<MyTextInput value={exampleTextValue2} onChange={handleChange2} />
<MyTextInput value={exampleTextValue2} onChange={handleChange2} />
{/* to feel the impact, copy the above line like 98 more times */}
</div>
</>
);
}