亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React Native Flatlist 在上拉時不刷新數據

React Native Flatlist 在上拉時不刷新數據

HUH函數 2021-10-29 09:54:43
這是我的帖子頁面代碼,它在加載時從我的 API 獲取帖子標題,這很完美。問題是,一旦它被加載,如果一個新帖子被添加到 API 并且我拉起來刷新它不會得到新帖子,即使我的 onRefresh 函數工作,因為我可以在其中觸發警報。我可以在 API 中獲取新帖子以在加載后顯示的唯一方法是重新加載應用程序本身。componentDidMount() {    this.fetchData()  }  constructor(props) {    super(props);    this.state = {      refreshing: true,      data: []    };  }fetchData = () => {    const url = 'myAPIurl';    fetch(url)      .then(res => {        return res.json()      })      .then(res => {        const arrayData = [...this.state.data, ...res]        this.setState({          data: arrayData,          refreshing: false        });      })      .catch(error => {        console.log(error);        this.setState({ refreshing: false });      });  };handleRefresh = () => {  this.setState(    {      refreshing: true    },    () => {      this.fetchData();      alert('Pulled Up to Refresh');    }  );};  render() {    return (<View>      <FlatList        refreshControl={          <RefreshControl            refreshing={this.state.refreshing}            onRefresh={this.handleRefresh}          />        }        horizontal={false}        data={this.state.data}        keyExtractor={item => item.id}        renderItem={({ item }) =>    <View>          <Text>{item.title.rendered}</Text>     </View>        }      /></View>    );  }}當我拉起刷新時,我收到此警告:兩個具有相同密鑰的孩子。鍵應該是唯一的。這很奇怪,因為每個帖子 ID 都是唯一的。即使有這個警告,API 中的新帖子也不會顯示,除非我重新加載應用程序。
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

更改您的handleRefresh功能,如下所示:


handleRefresh = () => {

this.setState(

{

  refreshing: true,

  data:[]

},

() => {

  this.fetchData();

  alert('Pulled Up to Refresh');

  }

 );

};


查看完整回答
反對 回復 2021-10-29
  • 1 回答
  • 0 關注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號