Home > Back-end >  React Functional Component - Use Callback and Props at Same Time
React Functional Component - Use Callback and Props at Same Time

Time:10-04

I'm trying to learn React JSX as pretty new. I want to use Callback function from my Parent component and props at same time but I couldn't handle with this. I've tried a lot of combinations like ({childToParent}, props) - ({childToParent, props})

I always gets an different error. I just want to define my "childToParent" callback and props on my Child component. May anyone help me please? Thanks a lot!

P.S: List component is my child component's name

export default function List(props, {childToParent}) {

....

} 

CodePudding user response:

Put the callback into the props:

// Parent calls child:
<List someProp="foo" childToParent={someCallback} />
// Child:
const List = ({ someProp, childToParent }) => {
  // use someProp and childToParent here
};
  • Related