gpt4 book ai didi

javascript - React Native - 如何在返回上一页时刷新 FlatList?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:19 26 4
gpt4 key购买 nike

我将尝试解释我的情况:

我有 2 个页面/屏幕

SreenA.js :

...
<FlatList>
</FlatList>
...

ScreenB.js :

...
<Button> This button for back to ScreenA </Button>
...

我想要实现的目标:

When i press back button in screenB , my app will back to screenA. When i come back to screenA , i need to refresh the FlatList. (each time i back to screenA).

我如何在 Android Native 中实现这种情况?

I add this section to help you to more understand what challenge i face. In Android Native, i will use onResume to refresh the list (onResume called everytime the screen/page called).

我已经尝试但现在对我不起作用的方法:

我已经尝试过了,但是当应用程序在后台然后在前台显示时,我得到的只是调用:

state = {
appState: AppState.currentState
}

componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}

componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}

_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
//i place the code here for refresh the flatList. But not work
}
this.setState({appState: nextAppState});
}

最佳答案

添加 navigation.addListener 以在每次页面接收到导航焦点时触发。

举个例子:

class Page1 extends React.Component {
constructor(props) {
super(props);
this.state = { ts: 0 }

// this will fire every time Page 1 receives navigation focus
this.props.navigation.addListener('willFocus', () => {
this.setState({ ts: Date.now() })
})
}

render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Page 1 [{this.state.ts}]
</Text>
<Button title="Page 2" onPress={() => this.props.navigation.navigate('Page2')}/>
</View>
);
}
}

有关完整示例,请查看以下 snack在你的手机上。每次您导航回第 1 页时,时间戳都会更新,但不会在应用恢复时更新。

您应该能够根据您的需要从这个示例中扩展它。

关于javascript - React Native - 如何在返回上一页时刷新 FlatList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com