2016-09-27 63 views
1

我的問題也正在於此: https://gist.github.com/somanythings/8c3d34de754af311d7826ea837d160b4在與scalajs scalajs - 反應如何將一個scalajs定義的組件傳遞給一個javascript定義的組件?

在使用scalajs與japgolly的scalajs反應的(https://github.com/japgolly/scalajs-react)庫。我試圖包裝扒爐網格http://griddlegriddle.github.io/Griddle/customization.html 我想要一個自定義列,並且要做到這一點,需要傳遞包含組件的columnMetadata結構。

當我這樣做時,我可以呈現一個沒有屬性的scalajs定義組件,但是如果我嘗試通過renderP訪問屬性,或者通過renderS訪問屬性,則它們都未在render函數的作用域中定義。如果我在瀏覽器中進行調試,它們的名字是綁定的,並且具有預期的值。

當我

def renderP(f: (DuringCallbackU[P, S, B], P) => ReactElement): Out = 
    render($ => f($, $.props)) 

打破然後$.props是不確定的

我缺少什麼?這是ReactComponentB調度中的一個簡單的打字問題。 它與https://github.com/japgolly/scalajs-react/issues/157有某種關係,我剛剛沒有看到如何?

// Confusion over how to pass a scalajs defined component to a javascript defined component 

object GriddleComponentWrapper { 
    // for customComponent I've tried js.Any, ReactComponentU 
    @ScalaJSDefined 
    class ColumnMeta(val columnName: String, val order: Int, val customComponent: ReactClass=null) extends js.Object 
} 

case class GriddleComponentWrapper(results: js.Any, //Seq[Map[String, Any]], 
            columns: Seq[String], 
            columnMeta: Option[Seq[ColumnMeta]] = None, 
            showSettings: Boolean = true, 
            showFilter: Boolean = true 
           ) { 
    def toJS = { 
    val p = js.Dynamic.literal() 
    p.updateDynamic("results")(results) 
    p.updateDynamic("columns")(columns) 
    p.updateDynamic("showSettings")(showSettings) 
    p.updateDynamic("showFilter")(showFilter) 

    (columnMeta).foreach { case cm => p.updateDynamic("columnMetadata")(cm.toJsArray) } 

    p 
    } 

    def apply(children: ReactNode*) = { 
    val f = React.asInstanceOf[js.Dynamic].createFactory(js.Dynamic.global.Bundle.griddle) // access real js component , make sure you wrap with createFactory (this is needed from 0.13 onwards) 
    f(toJS, children.toJsArray).asInstanceOf[ReactComponentU_] 
    } 

} 

object MyTestGrid { 

    @js.native 
    class ColumnMetaProps(val data: js.Object, val rowData: js.Object, val metadata: js.Object) extends js.Object 

    // I've tried making the Props argument js.Dynamic, and also the ColumnMetaProps above 
    @JSExport 
    val testComp = ReactComponentB[js.Dynamic]("Mine").renderP(
    (sc, props: js.Dynamic) => { 
     //when debugging this in the browser, 'sc' and 'props' have inspectable object values with the expected members in the browser 
     //dev tools, BUT, they're undefined 
     log.info(s"what is ${sc.props}") 
     log.info(s"what is $props") 
     val string: Frag = if (!js.isUndefined(props)) props.data.toString() else "nothing!" 
     <.h1(string) 
    }).build 

    @JSExport 
    val aCompletelyStaticComponentWithNoPropsWillWork = ReactComponentB[js.Dynamic]("MyStaticComponent").renderP(
    (sc, props: js.Dynamic) => <.h1("this renders!!")).build 


// am I passing the right thing to columnmeta with testComp.reactClass? 
val columnMeta = (new ColumnMeta("c1", 1, testComp.reactClass) :: Nil).toJsArray 

    val results = Seq(
    js.Dynamic.literal("c1" -> "row1c1", "c2" -> "row1c2"), 
    ).toJsArray 

    val component = ReactComponentB[js.Dynamic]("MyTestGrid") 
    .render_P { 
     props => 
     GriddleComponentWrapper(results, columns = "c1" :: "c2" :: Nil, columnMeta = Some(columnMeta))() 
    }.build 

    def apply() = component 
} 
+0

不是一個解決問題的方法,而是一種解決方法,所以我在評論而不是'回答',希望它能幫助別人。我的定製組件如下: 'val myCustomComp:js.Function = <.h1(s「some wrapper $ {props.data.toString()}」)。render }' 然後將myCustomComp傳遞給列元數據 – lbp

+0

我對gitter提出了同樣的問題:https://gitter.im/japgolly/scalajs-react?at = 57fdda6bdfe82a365b1a71ad – jhegedus

+0

你在哪裏將一個scalajs組件傳遞給一個js組件? https://gist.github.com/somanythings/8c3d34de754af311d7826ea837d160b4?所有我能看到的是scalajs代碼...你的意思是https://gist.github.com/somanythings/8c3d34de754af311d7826ea837d160b4#file-scalajsreactinterop-scala-L57? – jhegedus

回答

3

React.JS要求道具和狀態總是一個對象(或null)。使用單個Scala值(如原始或案例類實例)會在React.JS中導致異常。因此,在scalajs反應中,爲了允許用戶以類型安全的方式使用任何道具/狀態類型,在引擎蓋下使用具有單個鍵"v"的對象。即不是直接使用123,而是使用{v:123}

您可能需要在代碼中適應該裝箱。

現在,在下一個主要版本(見「新」分支)中,組件的表示大大改善,使得沒有像我剛纔描述的那樣更隱藏的魔法。儘管v0.x的設計並不是爲了促進JS↔Scala組件的互操作性,但它將是明確的,明顯的,並且有希望在neo中微不足道。

+0

這是什麼意思在實踐中?有一個簡單的例子,我該怎麼做? – jhegedus

+0

我個人不知道一個例子,但我建議在scalajs中提出問題 - 反應gitter聊天室。我認爲那裏有一兩個人有類似的用例。此外,如果您找到或創建示例,請考慮將其提供給scalajs-react的文檔以幫助未來同一問題的其他人。祝你好運。 – Golly

+0

嗨,感謝您的提示,我做到了這一點,並得到了幫助,我在這裏記錄瞭解決方案:https://github.com/jhegedus42/scalajs-react-playaround/wiki/ReactJS-----ScalaJS-React -Interop – jhegedus

相關問題