Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native: remove item from ListView

Tags:

react-native

I'm using React Native (0.12.0) on Android and have a ListView component on my page. I want to be able to remove items from the list.

This ListView looks like this:

<ListView
    dataSource={this.state.dataSource}
    renderRow={this._renderStory}
    onEndReached={this._getStories}
    />

The rowHasChanged method looks like this.

this.state = {
    dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => {
            return row1 !== row2;
        },
    }),
    loaded: false,
};

All of that works just fine.

Now, to remove an item I have this code:

var indexOfItem = this._data.findIndex((item) => item.id === story.id);
this._data.splice(indexOfItem, 1);

this.setState({
    dataSource: this.state.dataSource.cloneWithRows(this._data),
});

I can confirm that the item is being removed from this._data. this.state.dataSource._cachedRowCount is reduced by one as it should be. The state is being updated and a render cycle triggered. But the item isn't removed from the page. What's more, if I put a console log in this._renderStory(), the only thing happening is it renders a new row at the bottom of the list.

Am I missing something? Is there another step I need to do?

A similar question was asked here but he never got an answer about removing a row. How to add/Delete item into ListView?

All the other examples I've seen around the place lack an example of removing an item.

UPDATE: Thanks, it required passing a clone of the array AND setting a key (sorry can only accept one answer). But this meant the whole list was being rendered. In the end I just set a 'removed' property on the items, tested for that property changing and returned null in the render method if an item was flagged as remove, this was faster than re-rendering all items.

like image 968
David Gilbertson Avatar asked Oct 11 '15 00:10

David Gilbertson


People also ask

How do you delete a specific item from list in React native?

To delete an item from list with React and JavaScript, we can use some array methods. to create the items state with useState . Then we define the deleteItem function that takes the index of the item to delete and returns a function that calls setItems with (items) => items. filter((_, i) => i !==

How do you remove an element from an array in React?

To remove an element from an array of objects in React: Use the filter() method to iterate over the array. On each iteration check if a certain condition is met. The filter method returns an array containing only the elements that satisfy the condition.

How to remove an item from a list in react?

It's a common task in React to remove an item from a list. Here I want to show you briefly how this works. Every time you want to modify something in React, for example a list where you want to remove an item, you have to use React's state management.

How do you display a list in React Native?

Using List Views React Native provides a suite of components for presenting lists of data. Generally, you'll want to use either FlatList or SectionList. The FlatList component displays a scrolling list of changing, but similarly structured, data.

How to add or remove a list item in listview?

You can add or remove list items from the ListView component using the addItem and removeItem methods. Refer to the following steps to add or remove a list item. Render the ListView with data source, and use the template property to append the delete icon for each list item.

How to remove the list item within the click event?

Within the click event, remove the list item by passing the delete icon list item to removeItem method. import * as React from 'react'; import * as ReactDOM from "react-dom"; import { ListViewComponent } from '@syncfusion/ej2-react-lists'; import { ButtonComponent } from '@syncfusion/ej2-react-buttons'; export default class App extends React.


Video Answer


2 Answers

<ListView
  key={this._data}
  dataSource={this.state.dataSource}
  renderRow={this._renderStory}
  onEndReached={this._getStories}
/>

Set the key of the ListView with this._data. If you have an Key/ID array of rows, use it like key={this._data.IDArray}.

See ListView removing wrong row on dataSource update for more details.

like image 87
KChen Avatar answered Jan 04 '23 12:01

KChen


In order for the list to update, you need to pass to it a duplicate of the array, not the same array. So in your case, something like this:

this.setState({
  dataSource: state.dataSource.cloneWithRows(this._data.slice(0))
});
like image 38
eyal83 Avatar answered Jan 04 '23 11:01

eyal83