I have a long list of components which takes a value from my Context.
Component has a unique id, and also knows which is the "current" id. I only want the Component whose id matches the currentId to update when the value from myContext changes.
const ComponentList = () => {
return(
<>
{data.map((d,i) =>
<Component key={i} id={d.id} />
}
</>
)
}
const Component = ({id}) => {
const {value} = useContext(MyContext);
const {currentId} = useContext(OtherContext);
//only re-render if `id === currentId` and `value` changes
return (
<h1>{value}</h1>
)
}
Note that the value from myContext does not care about which Component id is the current id.
I figure the answer lies somewhere in memoization but I haven't been successful in finding it. (I was able to get this working as I intend by doing const {value} = id === currentId ? useContext(MyContext) : {value: null} but this is obviously not okay)
I've abstracted this to a very generic example but if more context (haha) is necessary I can explain my use case further.