Last active
February 8, 2019 17:39
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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