scala.jsscalajs-react

How to render a React.Suspense?


I'm trying to render a React.Suspense on an empty page, but failed with the following error:

Uncaught Invariant Violation: Element type is invalid: expected a
string (for built-in components) or a class/function (for composite
components) but got: object.

Here is the code. It compiles. Does that mean scalajs-react or scalajs is not 100% type safe? What should I do to fix the problem?

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)
  }
}

Here are the project files.

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>

Solution

  • This seems to be an issue with how scalajs-react was declaring its imports.

    v1.4.1 is on its way to Maven Central now and should fix this.

    See https://github.com/japgolly/scalajs-react/issues/522 for detail.