Home > Net >  How to call a function inside a component in a custom hook?
How to call a function inside a component in a custom hook?

Time:12-10

I have a component which has a function in it. also I have a custom hook name "useHook-A" I want to call my function which is inside the component through my hook "A". I dont know how to call that function. I cant export it and I cant pass it as props.

const MyComponent = () =>{
   const myFunction = () =>{}
}
export default Mycomponent

const useHook-A = () =>{

  // Here I want to call myFunction which is inside the MyComponent
}
export default useHook-A

CodePudding user response:

Simply pass your function as a parameter of your hook:

const MyComponent = () => {
  const myFunction = () => {};
  useHook-A(myFunction);
}
const useHook-A = (func) => {
  // func();
}
  • Related