I created a react app, in which I have a list of items with delete action. I know that if i pass this delete action from parent (the items container) , i can use "useCallback" in the parent to prevent unneeded recreations of the delete method.
But, what will happen if ill define the delete method in the item component and use "useCallback" in it? wont it work? wont it use the same memorized method without creating each time a new one?
in other words, what is the scope of the memorization? is it global? or per component?
pseudo code:
function ListComponent(){
let arr = [1,2,3]
return <div>
{
arr.map(item => <Item id={item} />
}
</div>
}
function Item({id}){
let memorizedDelete = useCallback(() => alert("deleting"))
return <div onClick={memorizedDelete} > {id} </div>
}
thnx