question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding and Removing items

See original GitHub issue

I’m trying to add and remove items from the lists, using onClick on buttons. Doing Add was easy using the SortableComponent, but I don’t know how to use remove because SortableList and SortableItem are functions not classes to add a method(code below and attached file). I’m just starting working, with React ES6, maybe is something that I don’t understand.

Thanks.

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

class SortableComponent extends React.Component{
     constructor(props) {
         super(props);
         this.state = {items: [1, 2, 3, 4]};
         this.onSortEnd = this.onSortEnd.bind(this);
         this.add = this.add.bind(this);
     }


    onSortEnd({oldIndex, newIndex}) {
        this.setState({items: arrayMove(this.state.items, oldIndex, newIndex)});
    }


    add() {
        const items = this.state.items;
        let new_item = this.state.items.length+1;
        items.push(new_item);
        this.setState({items : items})
        console.log(new_item);
    }

    render() {
        return (
            <div>
                <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />
                <button onClick={this.add}>Add Document</button>
            </div>
        );
    }
}

example

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
benbonnetcommented, Sep 28, 2017

@badaljain encountered the very same problem, solved it by specifying another ‘index’ prop :

const SortableItem = SortableElement(({idx, node, onRemove}) =>
  <li onClick={() => onRemove(idx)}>
    <DragHandle />
    {node.title}{idx}
  </li>
);
const SortableList = SortableContainer(({items, onRemove}) => {
  return (
    <ul>
      {items.map( function(node, index){
        console.log(index)
        return(<SortableItem key={`item-${index}`} index={index} idx={index} node={node} onRemove={onRemove} />)
      })}
    </ul>
  );
});

quite ugly compared to the original solution, not exactly sure why the index prop was not going though, but this way it works.

0reactions
bciachcommented, Nov 13, 2017

Solved my issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add or remove items from a drop-down list - Microsoft Support
Do any of the following: To add an item, go to the end of the list and type the new item. To remove...
Read more >
All the Ways to Add and Remove Items from a List in Python
We first start with built-in methods to add an item. In Python, there are three built-in methods to add items to a list....
Read more >
How to remove an added list items using JavaScript
The list items are added or removed using JavaScript functions addItem() and removeItem(). The list items are created using document.
Read more >
How to add and remove items from a list in Python - Linux Hint
remove (item): This method is used to remove particular item from the list. pop (index): The method is used to remove item from...
Read more >
Adding and Removing Elements From Arrays in JavaScript
Remove and Replace Items With splice(). You can use splice() to insert a value inside an array or to replace a value with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found