Search code examples
scalaplayframeworkscala.jsscalajs-reactdiode

Rendering React (scalajs-react) component with Diode error


I'm going to use Diode to manage application state with unidirectional data flow. I use Playframework 2.4 and Scala.js and here is a part of build.sbt for client subproject:

  libraryDependencies ++= Seq(
    ...
    "me.chrons" %%% "diode" % "1.0.0",
    "me.chrons" %%% "diode-react" % "1.0.0",
    "com.github.japgolly.scalajs-react" %%% "core"  % "0.11.3",
    "com.github.japgolly.scalajs-react" %%% "extra" % "0.11.3"
  ),

I am able to run basic samples from scalajs-react and decided to extend Timer sample with Diode support. I wrote a Curcuit object:

object TimerCircuit extends Circuit[TimerRootModel] with ReactConnector[TimerRootModel] {
  // initialModel and actionHanler implemented here
}

Tutorials and samples don't show us how to render React components using ReactDOM.render(...), but only using package japgolly.scalajs.react.extra.router.

However, I don't want to use a router this time and want just to render my component into HTML root tag like this:

class ReactStateSampleView() {

  case class State(secondsElapsed: Long)

  class Backend($: BackendScope[ModelProxy[String], State]) {
    val DefaultInterval: Long = 1000

    //noinspection ScalaStyle
    var interval: js.UndefOr[js.timers.SetIntervalHandle] = js.undefined

    def tick: Callback = $.modState(s => State(s.secondsElapsed + 1))

    def start: Callback = Callback {
      interval = js.timers.setInterval(DefaultInterval)(tick.runNow())
    }

    def clear: Callback = Callback {
      interval foreach js.timers.clearInterval
      interval = js.undefined
    }

    def render(s: State): ReactTag =
      <.div("Seconds elapsed: ", s.secondsElapsed)
  }

  val timer = ReactComponentB[ModelProxy[String]]("Timer")
    .initialState(State(0))
    .renderBackend[Backend]
    .componentDidMount(_.backend.start)
    .componentWillMount(_.backend.clear)
    .build

  val sc = TimerCircuit.connect(_.data)

  ReactDOM.render(sc(p => timer(p)), dom.document.getElementById("timer"))

but this part

ReactDOM.render(sc(p => timer(p)), dom.document.getElementById("timer"))

doesn't work. Project doesn't compile and shows "RuntimeException: There were linking errors"

How to solve the problem?

Update

Here is the exact message of the linking error and stack trace:

[info] Fast optimizing C:\tmp\project\client\target\scala-2.11\client-fastopt.js
[error] Referring to non-existent class japgolly.scalajs.react.Callback$ResultGuard$
[error]   called from diode.react.ReactConnector$$anonfun$wrap$1.apply(java.lang.Object)scala.Function0
[error]   called from diode.react.ReactConnector$$anonfun$wrap$1.apply(java.lang.Object)java.lang.Object
[error]   called from japgolly.scalajs.react.Internal$FnComposer$$anonfun$apply$2$$anonfun$apply$3.apply(java.lang.Object)java.lang.Object
[error]   called from japgolly.scalajs.react.CompState$RootAccessor.modState(japgolly.scalajs.react.CompScope$CanSetState,scala.Function1,scala.Function0)scala.Unit
[error]   called from japgolly.scalajs.react.CompState$RootAccessor.modState(java.lang.Object,scala.Function1,scala.Function0)scala.Unit
[error]   called from japgolly.scalajs.react.CompState$WriteCallbackOps$class.modState(japgolly.scalajs.react.CompState$WriteCallbackOps,scala.Function1,scala.Function0)scala.Function0
[error]   called from japgolly.scalajs.react.CompState$ReadCallbackWriteCallback.modState(scala.Function1,scala.Function0)scala.Function0
[error]   called from org.example.client.playground.ReactStateSampleView$Backend.tick()scala.Function0
[error]   called from org.example.client.playground.ReactStateSampleView$Backend$$anonfun$start$1.apply$mcV$sp()scala.Unit
...
Caused by: java.lang.RuntimeException: There were linking errors
    at scala.sys.package$.error(package.scala:27) ~[scala-library-2.11.8.jar:na]
    at org.scalajs.core.tools.linker.frontend.BaseLinker.linkInternal(BaseLinker.scala:133) ~[na:na]
    at org.scalajs.core.tools.linker.frontend.BaseLinker.linkInternal(BaseLinker.scala:86) ~[na:na]
    at org.scalajs.core.tools.linker.frontend.LinkerFrontend$$anonfun$4.apply(LinkerFrontend.scala:54) ~[na:na]
    at org.scalajs.core.tools.linker.frontend.LinkerFrontend$$anonfun$4.apply(LinkerFrontend.scala:54) ~[na:na]
    at org.scalajs.core.tools.logging.Logger$class.time(Logger.scala:28) ~[na:na]
    at org.scalajs.sbtplugin.Loggers$SbtLoggerWrapper.time(Loggers.scala:7) ~[na:na]
    at org.scalajs.core.tools.linker.frontend.LinkerFrontend.link(LinkerFrontend.scala:53) ~[na:na]
    at org.scalajs.core.tools.linker.Linker$$anonfun$link$1.apply$mcV$sp(Linker.scala:50) ~[na:na]
    at org.scalajs.core.tools.linker.Linker$$anonfun$link$1.apply(Linker.scala:49) ~[na:na]

Solution

  • The linking errors suggest that there is a binary incompatibility between scalajs-react and Diode. This sounds plausible from your libraryDependencies, because:

    If there was a binary incompatible change in scalajs-react-core from 0.11.1 to 0.11.3, that would explain your issue.

    If I am right, you can fix this issue by upgrading to Diode 1.1.0, which uses scalajs-react-core 0.11.3.