Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.babylonhealth.ember.ui
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import org.scalajs.dom.console
import org.scalajs.dom.ext.Ajax
import slinky.core.Component
import slinky.core.annotations.react
import slinky.web.html.div
// Example of weird setSate, the console prints:
// onComplete: isCompleted: true
// Render: isCompleted: false
@react class MyComponent extends Component {
type Props = Unit
case class State(foo: Option[Future[String]])
def initialState = State(None)
def render = {
state.foo match {
case Some(f) =>
console.info(s"Render: isCompleted: ${f.isCompleted}")
case None =>
}
div("foo")
}
override def componentDidMount(): Unit = {
console.info("Did mount")
val f: Future[String] = Ajax.get("http://www.google.com").map(_ => "abc")
f.onComplete(result => {
assert(f.isCompleted)
console.info(s"onComplete: isCompleted: ${f.isCompleted}")
setState(State(foo = Some(f)))
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.