【问题标题】:how to write facade for reactjs 0.13 React.Component scalajs如何为 reactjs 0.13 React.Component scalajs 编写外观
【发布时间】:2015-03-29 02:22:46
【问题描述】:

React.Component 定义: https://facebook.github.io/react/docs/top-level-api.html#react.component

ReactComponent 接口:https://facebook.github.io/react/docs/component-api.html

示例:https://facebook.github.io/react/docs/reusable-components.html#es6-classes

到目前为止我尝试了什么..

object React  extends js.Object{

  type Component[P,S] = ReactComponent[P,S]

}

class ReactComponent[P,S] extends js.Object {

  def this(props : P) = this()

  def setState(newState : S) : Unit = js.native

  def forceUpdate() : Unit = js.native

}

当我尝试这个时

 class Hello extends React.Component {

  }

我得到 undefined is not a function 。我做错了!因为我想不出正确的方法...

【问题讨论】:

    标签: scala.js


    【解决方案1】:

    由于您定义了type Component[P,S] = ReactComponent[P,S],Scala.js 期望定义一个类ReactComponent

    您必须:

    • React 内定义 Component 本身
    • ReactComponent 上使用@JSName("React.Component")

    进一步,

    class Hello extends React.Component
    

    没有定义扩展React.Component 的类,而只是另一种外观类型。因此,它可能不会达到您的预期。

    【讨论】:

    • 那我怎样才能实现类似于 class Hello extends React.Component ...
    • 您目前无法使用 Scala.js 发出 ECMAScript 6 样式类。您可以像在 ECMAScript 5 中那样链接原型。但是,这可能会非常冗长。考虑使用 React 的 createClass 函数。
    • 嗯,我们已经有了 createClass 代码,作为升级 github.com/japgolly/scalajs-react/issues/95 的一部分,我正在尝试这个 :),我认为即使是 js 世界也使用 6 到 5 个转译器!
    猜你喜欢
    • 2019-11-11
    • 2018-08-29
    • 2016-01-05
    • 2015-04-16
    • 2018-12-05
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多