Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

got Type 'void' is not assignable to type '() => void' in react typescript

I tried to pass the props like

<Mycomp
 deleteAcc={handleDelete(accountId)}
/>

to another component. I write the props like

interface AccProps {
 deleteAcc: () => void;
}

and call the function like

deleteAcc()

But I got the below error

Type 'void' is not assignable to type '() => void'

Not sure what I have missed here.

like image 351
Gmv Avatar asked Jun 15 '26 12:06

Gmv


1 Answers

When you pass handleDelete(accountId) as a prop, the function is called immediately and its return value (in this case, a void) is the one being passed as a prop. If the function must take this particular argument, send it as a callback:

<Mycomp deleteAcc={() => handleDelete(accountId)} />

Since handleDelete(accountId) reutrn void, this callback is typed () => void and therefore suits the prop type.

If the function does not have to accept an argument, you can send it by name:

<Mycomp deleteAcc={handleDelete} />

Often a good idea is to create a unique wrapper function in the component and send it by name, for example:

const wrapper = () => {
  handleDelete(accountId);
};

<Mycomp deleteAcc={wrapper} />
like image 151
Ronny Efronny Avatar answered Jun 17 '26 01:06

Ronny Efronny



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!