immerjs / use-immer

Use immer to drive state with a React hooks
MIT License
4.04k stars 92 forks source link

return void draft.count++ in TS #37

Closed alexluong closed 4 years ago

alexluong commented 4 years ago

When using useImmerReducer, I have return void draft.count++ but it doesn't seem to work. I'm quite new to both immer and TypeScript. If anyone knows what's going on, I'd really appreciate your help.

Thank you.

danielkcz commented 4 years ago

Huh, I've never seen return void draft.count++ before :D Surprisingly enough, it's syntactically correct.

Ah, it's actually in README. Looks like a typo to me, should be without that void if anything.

Either way, with Immer you don't need to be returning anything, just mutate values and done.

The return in reducer is just for convenience because of the switch so you can exit that specific case. Without return or break it would fall-through to the next case.

mweststrate commented 4 years ago

"Doesn't seem to work" is unactionable. Please include actual relevant information/ reproduction / example rather than making us guess what might be wrong.

There is no code / error shared at all. Filing issues this way is typically will get them closed without resolution.

Op do 31 okt. 2019 06:34 schreef Daniel K. notifications@github.com:

Huh, I've never seen return void draft.count++ before :D Surprisingly enough, it's syntactically correct.

Either way, with Immer you don't need to be returning anything, just mutate values and done.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/immerjs/use-immer/issues/37?email_source=notifications&email_token=AAN4NBDSC34JRRD5INRXSADQRJ37BA5CNFSM4JHBPX7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECWWOVI#issuecomment-548235093, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN4NBAJPKR5PLRTWH3NKXLQRJ37BANCNFSM4JHBPX7A .

alexluong commented 4 years ago

@mweststrate you're right, my bad.

Like @FredyC suggested, not returning anything in the reducer function is fine. I didn't know that.

FWIW:

return void state.isOpen = true;

TS will underline = and says ';' expected.

If anyone wants to still use void, you can do:

return void (state.isOpen = true);

But again, this works perfectly fine too:

state.isOpen = true;
return;
shadoath commented 11 months ago

It would be good to update the readme to explain this.