Adding and Removing items
See original GitHub issueI’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>
);
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@badaljain encountered the very same problem, solved it by specifying another ‘index’ prop :
quite ugly compared to the original solution, not exactly sure why the index prop was not going though, but this way it works.
Solved my issue