简体   繁体   English

我们如何将行添加到反应本机列表视图中?

[英]How can we prepend rows to a react native list-view?

I've looked almost everywhere to find a solution to this! 我几乎到处寻找解决方案!

Is there a way of prepending rows to a listview without re-rendering every single row? 有没有办法在不重新渲染每一行的情况下将行添加到listview I find it bizarre that facebook haven't included a way to do this in there documentation! 我觉得很奇怪facebook没有在文档中包含这样做的方法! I feel as though there could be a memory efficient way of doing this as you can do it with the native scripts (java, swift). 我觉得可能有一种内存有效的方法,因为你可以用本机脚本(java,swift)来做到这一点。

There are a lot of discussions that try to solve it, such as this one: 有很多讨论试图解决它,比如这个:

https://github.com/facebook/react-native/issues/1682 https://github.com/facebook/react-native/issues/1682

But as of yet I haven't found an efficient way of doing this. 但到目前为止,我还没有找到一种有效的方法。

There is no direct method to prepend rows to your ListView. 没有直接的方法将行添加到ListView。 There is not much documentation on ListView.DataSource out there. ListView.DataSource上没有太多文档。 So here it goes- 所以这里 -

If you want to add/append/prepend/update/delete rows from a ListView, all you have to do is just update the dataSource and call cloneWithRows() in setState. 如果要从ListView添加/追加/前置/更新/删除行,您只需更新dataSource并在setState中调用cloneWithRows()。

For eg. 例如。 I want to fetch data from a URL. 我想从URL获取数据。 Each time I hit "Load more", I append new rows to my data source. 每次我点击“加载更多”时,我都会在我的数据源中添加新行。

getInitialState: function(){
    return {
        rawData: [],
        dataSource: new ListView.DataSource({
          rowHasChanged: (row1, row2) => row1 !== row2
        }),
        loaded: false,
        page: 1,
    }
},

At the very first time, I call this function in ComponentDidMount() which populates my ListView with initial data and "page"=1 in the state. 在第一次,我在ComponentDidMount()中调用此函数,该函数使用初始数据填充ListView,并在状态中填充“page”= 1。

  reloadData: function(){
    this.setState({ loaded: false, rawData: [], page: 1 });

    API.getUpcomingMovies(this.state.page)
      .then((data) => {
          this.setState({
            rawData: this.state.rawData.concat(data),
            dataSource: this.state.dataSource.cloneWithRows(this.state.rawData.concat(data)),
            loaded: true,
          });
      });
  },

And every time I tap on "load more" to get JSON from the API, I call the following function. 每次我点击“加载更多”以从API获取JSON时,我都会调用以下函数。 Where "page" is incremented every time I hit "load more". 每当我点击“加载更多”时,“页面”会增加。

fetchData: function(){

    this.setState({ loaded: false, page: this.state.page+1 });

    API.getUpcomingMovies(this.state.page)
        .then((data) => {
            this.setState({
              rawData: this.state.rawData.concat(data),
              dataSource: this.state.dataSource.cloneWithRows(this.state.rawData.concat(data)),
              loaded: true,
            });
        });
  },

As you can see, I am concatenating my rawData[] (which is a simple array holding JSON objects). 如您所见,我正在连接我的rawData [](这是一个包含JSON对象的简单数组)。 If you want to prepend new data to your rawData, you can do something like this- 如果您想在rawData中添加新数据,可以执行以下操作 -

this.setState({
    rawData: data.concat(this.state.rawData)
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM