我将使用 Diode 来管理具有单向数据流的应用程序状态。我使用 Playframework 2.4 和 Scala.js,这是build.sbt
客户端子项目的一部分:
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"
),
我能够从 scalajs-react 运行基本示例,并决定使用二极管支持扩展Timer 示例。我写了一个Curcuit
对象:
object TimerCircuit extends Circuit[TimerRootModel] with ReactConnector[TimerRootModel] {
// initialModel and actionHanler implemented here
}
教程和示例没有向我们展示如何使用 渲染 React 组件ReactDOM.render(...)
,而只是使用 package japgolly.scalajs.react.extra.router
。
但是,这次我不想使用路由器,只想将我的组件呈现为 HTML 根标记,如下所示:
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"))
但这部分
ReactDOM.render(sc(p => timer(p)), dom.document.getElementById("timer"))
不起作用。项目无法编译并显示"RuntimeException: There were linking errors"
如何解决问题?
更新
这是链接错误和堆栈跟踪的确切消息:
[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]