简体   繁体   English

React Native 如何清空数据源

[英]React Native how to empty dataSource

I want to simply delete the dataSource which is present in the state when I switch to a page.我想简单地删除切换到页面时状态中存在的数据源。 Because all previous responses are still present in the state every time I fetch data.因为每次我获取数据时,所有先前的响应仍然存在于状态中。 Everytime data is appended, but I want start with dataSource when I first open a page.每次附加数据时,但我想在第一次打开页面时从 dataSource 开始。

So, first of all I set dataSource and response in the constructor to null.因此,首先我将构造函数中的 dataSource 和 response 设置为 null。

constructor(props) {
    super(props);
    this.fetchData = this._fetchData.bind(this); 

    this.state = {
      dataSource: null, 
      response: null,
    };    
  }

This is how I fetch the data via an api and get the response.这就是我通过 api 获取数据并获得响应的方式。

_fetchData(callback) {
   ...
   fetch(`http://www.example.com/apifetch.php?etc`)
      .then(response => response.json())
      .then(callback)
      .catch(error => {
        console.error(error);
      });

 }

And in the componentDidMount I'm placing the json response in the state with setState.在 componentDidMount 中,我将 json 响应置于 setState 状态。

 componentDidMount() {

    this.fetchData(responseJson => {            
      let ds = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2,
      }); 
      const data = responseJson.results;
        console.log(responseJson);

      this.setState({
        dataSource: ds.cloneWithRows(data),
        isLoading: false,
        _data: data,
        _dataAfter: responseJson.after,
        response: data,
      });
    });

  }

When I render this.state.dataSource it shows the new and all the previous responses.当我呈现 this.state.dataSource 时,它​​会显示新的和所有以前的响应。 Now what is the best way to delete the dataSource the first time so all the previous data is gone and how can it be done (without using packages like eg Redux)?现在第一次删除数据源的最佳方法是什么,以便所有以前的数据都消失了,如何完成(不使用诸如 Redux 之类的包)?

UPDATE I am specifically searching for is a way that when a page is re-rendered and a new ListView.DataSource is initialized, the previous DataSource that is stored in the state will be deleted. UPDATE我专门搜索的是一种当重新渲染页面并初始化新的 ListView.DataSource 时,将删除存储在状态中的先前 DataSource 的方法。

Now multiple dataBlobs are stored in the state after every render.现在,在每次渲染后,多个 dataBlob 都存储在状态中。

 {"_dataBlob":{"s1":[{"id":"1","key_id":"1","title":"title","image":{"uri":"http://www.example.com/1.jpg"}}]},{"id":"2","key_id":"2","title":"title","image":{"uri":"http://www.example.com/2.jpg"}}]}"_dirtyRows":[[true,true]],"_dirtySections":[false],"_cachedRowCount":2,"rowIdentities":[["0","1"]],"sectionIdentities":["s1"]} 

 {"_dataBlob":{"s1":[{"id":"3","key_id":"3","title":"title","image":{"uri":"http://www.example.com/3.jpg"}}]},{"id":"2","key_id":"2","title":"title","image":{"uri":"http://www.example.com/4.jpg"}}]}"_dirtyRows":[[true,true]],"_dirtySections":[false],"_cachedRowCount":2,"rowIdentities":[["0","1"]],"sectionIdentities":["s1"]} 

This way the performance will decrease when a user continuousely visits different pages.这样,当用户连续访问不同的页面时,性能会下降。 Is there a solution, or is this merely the downside of using ListViews in contrast to eg FlatLists?有没有解决方案,或者这仅仅是使用 ListViews 与 FlatLists 相比的缺点?

what you can do is just set dataSource value to null before you do api call in componentDidMount() like this:您可以做的只是在像这样在 componentDidMount() 中调用 api 之前将 dataSource 值设置为 null:

 componentDidMount() {

   //set null value to your dataSource 
        this.setState({
          dataSource: null, 
         })

            this.fetchData(responseJson => {            
              let ds = new ListView.DataSource({
                rowHasChanged: (r1, r2) => r1 !== r2,
              }); 
              const data = responseJson.results;
                console.log(responseJson);

              this.setState({
                dataSource: ds.cloneWithRows(data),
                isLoading: false,
                _data: data,
                _dataAfter: responseJson.after,
                response: data,
              });
            });

          }

Let me know does it work for you or not :)让我知道它是否对你有用:)

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

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