【问题标题】:Using a higher order react component from scalajs-react使用来自 scalajs-react 的高阶反应组件
【发布时间】:2017-02-16 16:29:08
【问题描述】:

我正在尝试使用 this higher order 使用 scalajs-react 库的 Scala.js 反应组件。

这是一个关于如何在 JS 中使用该组件的示例:

import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';

const SortableItem = SortableElement(({value}) => <li>{value}</li>);

const SortableList = SortableContainer(({items}) => {
    return (
        <ul>
            {items.map((value, index) =>
                <SortableItem key={`item-${index}`} index={index} value={value} />
            )}
        </ul>
    );
});

class SortableComponent extends Component {
    state = {
        items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6']
    }
    onSortEnd = ({oldIndex, newIndex}) => {
        this.setState({
            items: arrayMove(this.state.items, oldIndex, newIndex)
        });
    };
    render() {
        return (
            <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />
        )
    }
}

render(<SortableComponent/>, document.getElementById('root'));

表达式 ({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt; 脱糖到 (props) =&gt; { var value = props.value; return &lt;li&gt;{value}&lt;/li&gt; }&lt;li&gt;{value}&lt;/li&gt; JSX 脱糖到 React.createElement("li", null, value ); 但问题是我不知道如何将这个表达式 (React.createElement("li", null, value );) 翻译成 Scala.JS。

({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt; 最终以 here 作为包装组件)

我需要在 Scala.JS 中写什么才能得到 React.createElement("li", null, value ) 的等价物?

总结:

换句话说,如果在 JS 中我写了 var element=React.createElement("li", null, value ) 并且生成的对象称为 element,那么计算出完全相同的 element 对象(val element = ???)的 Scala.JS 表达式是什么?

相关问题是here

【问题讨论】:

    标签: reactjs scala.js scalajs-react


    【解决方案1】:
    object Test
    {
      def createItem(itemText: String): ReactTagOf[LI] = <.li(_react_fragReactNode(itemText)(reactNodeInhabitableS))
    }
    
    object ScalaJSExample extends js.JSApp {
      def main(): Unit = {
    
        val item = Test.createItem("bla42")
        val item2: ReactElement = item
    
    }
    

    item2React.createElement("li", null, "bla42" ) 相同

    通过在浏览器中检查 item2React.createElement("li", null, "bla42" ) 确认:

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2019-11-01
      • 2017-09-26
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多