1

Here it is written:

Included is a type CallbackTo[A] which captures effects designated for use in React callbacks.

What is a React callback in this context ?

sjrd
  • 21,805
  • 2
  • 61
  • 91
jhegedus
  • 20,244
  • 16
  • 99
  • 167

1 Answers1

1

As far as I know, just the normal meaning of callback - a function that is called in response to an event. So for example in a React Component there are many functions like componentDidMount that are called at different stages in the lifecycle of the component. In scalajs-react these are implemented as functions that return a CallbackTo[Unit], which can also be written as Callback due to a type alias. For example when adding a componentDidMount callback to a ReactComponentB, we use def componentDidMount(f: DuringCallbackM[P, S, B, N] => Callback): ReactComponentB[P, S, B, N].

In javascript a component is expected to just immediately run any side-effects of the event, in the componentDidMount function (or the other callbacks). In scalajs-react, the component instead wraps these effects in a Callback, and returns that. This allows for the scalajs-react system to delay actual execution of the code in the Callback - this is done later by calling runNow(). This also means that Callbacks are combined using map, flatMap, >> etc. They will not run unless runNow() is eventually called, so if you are not returning a Callback to some other code to execute later, just creating it will do nothing.

There's much more about Callback in the docs.

Matt R
  • 9,892
  • 10
  • 50
  • 83
trepidacious
  • 592
  • 4
  • 9