Declaring a function expression in parent or child component?

Viewed 38

Say I have a child component, a product card, and a parent component, a product page.

Each product card component should have edit and delete functionality.

I was thinking what would be the right approach: to create delete and edit functions inside product card component, because they are only related to this component, or create delete and edit functions inside product page component, then pass these function to children.

I think it's not a good idea to create function inside children, because functions will be recreated for each children, maybe I'm wrong?

1 Answers

If the function is only used in the child component and it doesn't need any parent component data then the best practice is to declare the function in the child component. If the function is used in many components and it depends on the parent state then you should declare the function in the parent component.

Related