Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call custom hooks conditionally react

I defined a custom hook to do some operations and calling some APIs like below

    const useCustomHook = (param1,param2) => {
      const response = callAPI(param1, param2 ){ .... }
       return [callAPI, response]
    }

and I used this hook somewhere and it works as well : for example

const [save ,response] = useCustomHook(param1,param2);

But, I need to call save with different parameters in different condition: for example :

if (type===1) 
    const [save ,response] = useCustomHook(param1,'a');
else 
    const [save ,response] = useCustomHook(param1,'b');

type is gotten from another hook.like this

   const type = useType()

However, as you know hooks can't call conditionally so the above code doesn't run.

Any ideas are appreciated.

like image 310
Hamid Shoja Avatar asked Feb 28 '26 12:02

Hamid Shoja


1 Answers

I think you should rather use the condition inside the custom hook. You can pass the type as third argument in the custom Hook and handle the conditional logic from there.

like image 122
abubakri olaitan Avatar answered Mar 03 '26 00:03

abubakri olaitan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!