I'll preface this question by saying I've spent about 3 weeks with React (previously have worked with Vue) so still pretty green.
I have a component structure like the following:
const initialState = { propertyA: null, propertyB: null };
const WrapperComponent: FC<Props> = (props) => {
const [dynamicObject, setDynamicObject] = React.useState(initialState);
const customCallbackFunction = (newObjectVal) => { setDynamicObject(newObjectVal) };
return (
<div>
<SiblingComponent dynamicObject={dynamicObject} />
<DifferentSiblingComponent onAction={customCallbackFunction} />
</div>
);
}
Problem I'm facing is each call to customCallbackFunction
is triggering re-render in both SiblingComponent
and DifferentSiblingComponent
. The re-render in SiblingComponent
is desired, because I want that component to display the dynamic data being emitted by customCallbackFunction
. However, I'd like to avoid the re-render of DifferentSiblingComponent
.
For more context, customCallbackFunction
is being fired on certain hover events on a canvas
- so the constant re-rendering is causing an infinite callback loop.
Is there a way to handle this without pulling in something like Redux? Any help/insight is appreciated.
Note: I did read that React.FC
is discouraged, that is what the team has used in the past so I was just following those templates