如何渲染 React.Suspense?
How to render a React.Suspense?
我正在尝试在空页面上呈现 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
build.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()
)
index.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>
这似乎是 scalajs-react 声明其导入的方式的问题。
v1.4.1 现在正在前往 Maven Central 的路上,应该会解决这个问题。
我正在尝试在空页面上呈现 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
build.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()
)
index.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>
这似乎是 scalajs-react 声明其导入的方式的问题。
v1.4.1 现在正在前往 Maven Central 的路上,应该会解决这个问题。