這是我的帖子頁面代碼,它在加載時從我的 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 中的新帖子也不會顯示,除非我重新加載應用程序。
React Native Flatlist 在上拉時不刷新數據
HUH函數
2021-10-29 09:54:43