maxwell
maxwell

Reputation: 2381

Using React Higher Order Component - Specifcally react-sortable-hoc

I am having trouble understanding a very basic part of this HOC that has a good traction on NPM so I am guessing there is an easy answer here:

Where do my pre-existing components go in the example he gives on NPM. Is {value} supposed to be replaced with <myComponent />? I know these wrappers are supposed to take my component, in this case components, as arguments, but I think I am missing something integral here. Any help appreciated. His example documentation is below

import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-list-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'));

Upvotes: 1

Views: 2538

Answers (1)

Damien Leroux
Damien Leroux

Reputation: 11693

SortableElement will wrap your component to benefit from react-sortable-hoc features. So you have to write something like that (Assuming <myComponent /> is an element of the list):

const SortableItem = SortableElement(myComponent);

This will create a list element that will render myComponent with additional features.

({value}) => <li>{value}</li> is a stateless function representing a React Component. You could have write:

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

Upvotes: 1

Related Questions