I am working on a project, where Parent has two Children – A & B.
childA has a list of buttons, and when the user clicks on one of them, I want to capture the ID of the button. HOWEVER, I don’t want childA to be rerendered.
What I want is for childB to receive the ID of the button, and render itself.
I believe using state is why every time a button is clicked, it will rerender ChildA. What’s the right way of achieving the desire behavior here?
(for context, I have simplified things to make the problem clear, in reality childA is a map, and childB is a diagram.)
export function Parent() {
[buttonId, setButtonId] = useState(null);
return (
<>
<ChildA buttonId={buttonId} onClick={(id) => setButtonId(id)} />
<ChildA inputId={buttonId} />
</>
);
}
export function ChildA({ buttonId, onClick }) {
return (
<>
<Button key='1' onClick={() => onClick(1)}>
<Button key='2' onClick={() => onClick(2)}>
<Button key='3' onClick={() => onClick(3)}>
<Button key='4' onClick={() => onClick(4)}>
<ElementIDoNotWantRerenderedWhenAButtonIsClicked />
</>
);
}
export function ChildB({ inputId }) {
return (
<>
<RenderMeElement inputId={inputId}>
</>
);
}
2
Answers
You can use conditional rendering in React JS. This should work for you
Parent.js
ChildA.js
ChildB.js
Notice that I have changed the onClick event from
onClick={onClick(1)}
toonClick={() => onClick(1)}
. This is the official documentation from React explaining it.I hope this helps.
You can use React’s
React.memo
to prevent unnecessary re-renders ofChildA
.React.memo
is a higher order component, it’s similar toReact.PureComponent
but for function components instead of classes. If your component renders the same result given the same props, you can wrap it in a call toReact.memo
for a performance boost in some cases by memoizing the result.Here’s how you can apply it to your
ChildA
component:https://react.dev/reference/react/memo
https://react.dev/reference/react/PureComponent
https://react.dev/learn/render-and-commit
https://react.dev/learn/state-a-components-memory