I am using useCallback
to memo my callback function but it doesn’t seem to work with currying function, my PhotoItem
component always re-render when MyListPhoto
re-render although I wrapping my PhotoItem in React.memo(). Because handleDelete
function is a currying function, is there any way to use with useCallback ?
const MyListPhoto = () => {
...other state...
const { delete } = useDeletePhoto();
// always create new function when component re-render
const handleDelete = useCallback((index) => (photoId) => {
delete(photoId, index)
}, []);
return (
<ul>
{photos.map((photo, index) => (
<PhotoItem key={photo.id} photo={photo} onDelete={handleDelete(index)} />
</ul>
)
}
PhotoItem component:
const PhotoItem = React.memo(({ photo, onDelete }) => {
console.log('re-render');
return (
...
)
})
2
Answers
You have to memoize each function
Sorry, I didn’t get the point! Why do you need to memorize the function?
how about this: