0

I am trying to use this higher order react component from Scala.js using the scalajs-react library.

Here is an example on how to use this component from 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'));

The expression ({value}) => <li>{value}</li> desugars to (props) => { var value = props.value; return <li>{value}</li> } and <li>{value}</li> JSX desugars to React.createElement("li", null, value ); but the problem is I don't know how to translate this expression (React.createElement("li", null, value );) into Scala.JS.

(({value}) => <li>{value}</li> ends up here as a wrapped component)

What do I need to write in Scala.JS so that I get the equivalent of React.createElement("li", null, value )?

Summary:

In other words, if in JS I write var element=React.createElement("li", null, value ) and the resulting object is called element, then what what is the Scala.JS expression that evaluates to the exact same element object (val element = ??? )?

Related question is here.

Community
  • 1
  • 1
jhegedus
  • 20,244
  • 16
  • 99
  • 167

1 Answers1

0
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

}

item2 is the same as React.createElement("li", null, "bla42" )

as confirmed by inspecting item2 and React.createElement("li", null, "bla42" ) in the browser:

enter image description here

jhegedus
  • 20,244
  • 16
  • 99
  • 167