question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error rendering component with React.Suspense

See original GitHub issue

Got an error with React.Suspense (React 16.7.0). Am I missing something?

import japgolly.scalajs.react._
import japgolly.scalajs.react.vdom.html_<^._
import org.scalajs.dom

object App {

  def main(args: Array[String]): Unit = {

    def body = {
      val main = AsyncCallback
        .pure(<.div(^.color := "#070", "Async load complete."))
        .delayMs(2000)

      React.Suspense(
        fallback = <.div(^.color.red, "Loading..."),
        asyncBody = main
      )
    }

    val Main =
      ScalaComponent
        .builder[Unit]("Main")
        .stateless
        .render_(<.div(^.fontSize := "175%", body))
        .build

    val container = dom.document.getElementById("root")
    Main().renderIntoDOM(container)
  }
}

Browser stack trace:

screen shot 2019-02-03 at 00 55 31

Replication repo: https://github.com/allantl/scalajs-react-suspense

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
teozkrcommented, Mar 14, 2019

This is because japgolly.scalajs.react.raw.Suspense is defined like this:

@JSImport("react", JSImport.Namespace, "React.Suspense")
@js.native
object Suspense extends js.Any

According to the documentation for the relevant overload of JSImport:

Namespace import (import the module itself), with a fallback on a global variable.
When linking with module support, this is equivalent to @JSImport(module, name).
When linking without module support, this is equivalent to @JSGlobal(globalFallback).

That means that if compiling for an environment without modules (such as a legacy browser environment) then it’s (correctly) interpreted as an alias for React.Suspense, but if compiled for an environment with modules then it gets interpreted as import * as Suspense from "react", which is obviously wrong.

I’ll send a PR as soon as I’ve verified that this fixes the issue.

0reactions
teozkrcommented, Mar 17, 2019

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suspense for Data Fetching (Experimental) - React
With Suspense, handling fetching errors works the same way as handling rendering errors — you can render an error boundary anywhere to “catch”...
Read more >
All You Need To Know About React Suspense - CopyCat Blog
A React fallback UI is a component rendered when an error occurs within React component tree or when a component is suspended due...
Read more >
10 Minute Read: Understanding React Suspense with Visuals ...
Any time a child component throws an error, such as a failed network request, a wrapping Error Boundary component can toggle its rendering...
Read more >
React Suspense and Error Boundary - Neethack
In React 16.6, React is adding the Suspense component that it can render fallback while the app is loading javascript or fetching API....
Read more >
A React component suspended while rendering, but no ...
It was using <Suspense /> element in the component but I removed it. Then application is crashed with above error. Is there any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found