Skip to content

Instantly share code, notes, and snippets.

@anthony-cros anthony-cros/MyApp.scala
Last active Feb 8, 2019

Embed
What would you like to do?
package minimal
import org.scalajs.dom
import scala.scalajs.js
object App {
def main(args: Array[String]): Unit = {
// prints to console as expected
println("hello on console")
val myElement: js.Any =
MyComponents.WelcomerCompanion.createElement(name = "Anthony")
val myContainer: dom.raw.Element =
org.scalajs.dom.document.getElementById("container")
Bindings.ReactDOM.render(
element = myElement,
container = myContainer)
}
}
// ===========================================================================
object MyComponents {
// @ScalaJSDefined is deprecated: add -P:scalajs:sjsDefinedByDefault to your scalac options and simply remove @ScalaJSDefined
@js.annotation.ScalaJSDefined // removing it actually fails...
class Welcomer(props2 /* !! calling it props causes issues !! */: js.Object)
extends Bindings.Component(
props = props2) {
val name: js.Dynamic = this.props.asInstanceOf[js.Dynamic].name /* uses scala.Dynamic's applyDynamic */
override def render() = s"hello ${name}"
}
// ---------------------------------------------------------------------------
object WelcomerCompanion /* !! calling it Welcomer causes issues !! */ {
def createElement(name: String) =
Bindings.React.createElement(
`type` = js.constructorOf[MyComponents.Welcomer],
config = js.Dynamic.literal(name = name /* arbitrary "keys" thanks to scala Dynamic */) )
}
}
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.