我正在尝试在空白页面上呈现 React.Suspense,但失败并出现以下错误:
Uncaught Invariant Violation: Element type is invalid: expected a
string (for built-in components) or a class/function (for composite
components) but got: object.
这是代码。它编译。这是否意味着 scalajs-react 或 scalajs 不是 100% 类型安全的?我应该怎么做才能解决问题?
package org.myorg
import japgolly.scalajs.react.{AsyncCallback, React, ScalaComponent}
import japgolly.scalajs.react.vdom.html_<^._
import org.scalajs.dom
object MyPage {
private val suspense = React.Suspense(
fallback = <.div("Loading..."),
asyncBody = AsyncCallback.point(<.div("Loaded!")).delayMs(1000))
private val component = ScalaComponent.builder[VdomElement]("Home")
.render_P(p => p)
.build
def main(args: Array[String]): Unit = {
val container = dom.document.getElementById("app")
// This line works
component(<.div("hello")).renderIntoDOM(container)
// Either of the following two lines fails
component(suspense).renderIntoDOM(container)
suspense.renderIntoDOM(container)
}
}
这是项目文件。
build.properties:
sbt.version=1.2.7
构建.sbt:
lazy val root = project.in(file("."))
.enablePlugins(ScalaJSPlugin)
.enablePlugins(ScalaJSBundlerPlugin)
.settings(
organization := "org.myorg",
scalaVersion := "2.12.8",
scalaJSUseMainModuleInitializer := true,
webpackBundlingMode := BundlingMode.LibraryOnly(),
emitSourceMaps := false,
npmDependencies in Compile ++= Seq(
"react" -> "16.8.4",
"react-dom" -> "16.8.4"
),
libraryDependencies ++= Seq(
"com.github.japgolly.scalajs-react" %%% "extra" % "1.4.0"
),
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()
)
索引.html:
<!doctype html>
<html lang="en" data-framework="scalajs-react">
<head>
<meta charset="utf-8">
</head>
<body>
<section id="app"></section>
<script src="../target/scala-2.12/scalajs-bundler/main/root-fastopt-library.js"></script>
<script src="../target/scala-2.12/scalajs-bundler/main/root-fastopt-loader.js"></script>
<script src="../target/scala-2.12/scalajs-bundler/main/root-fastopt.js"></script>
</body>
</html>