Skip to content
Advertisement

Equivalent of setState callback with React hooks

I have a modal with a list of answers. I can either click an answer to select it, then click a button to confirm my choice. Or I can double-click an answer to select it and confirm.

I’m having trouble properly handling the double-click case.

With React class components, I would have used setState()‘s callback like this:

JavaScript

But right now, I only figured out the following:

JavaScript

I strongly suspect that there’s a nicer/better way of doing it.

Maybe a simple:

JavaScript

Which way is better?

Advertisement

Answer

There is no similar set state in hooks (which fires callback after state is set). But, you could apply following refactor:

JavaScript

And then

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement