React Native重新加载页面 [英] React Native reload page
问题描述
所以我有一个带有活动的应用程序.该应用程序一开始会检查Internet连接.如果没有互联网连接,它将显示一个带有按钮的屏幕以刷新页面.问题是我的API调用(Axios)位于componentDidMount()
上,在第一次渲染后仅被调用一次.有什么办法可以重新加载页面,以便它再次调用componentDidMount
?
So I have an app with an activity. The app checks for Internet connection at the beginning. If there are no Internet connection, it will show a screen with a button to refresh the page. The problem is that my API calls (Axios) is located on componentDidMount()
where it's called only once after the first render. Is there any way I can reload the page so it calls componentDidMount
again?
我的意思是像完全刷新一样.我正在使用EXPO btw.任何帮助表示赞赏.抱歉,没有示例,我只是想知道这个想法
I mean like total refresh. I am using EXPO btw. Any help is appreciated. Sorry there are no examples, I just wanted to get the idea if possible
推荐答案
class MyScreen extends Component {
constructor(props) {
super(props)
this.state = {
lastRefresh: Date(Date.now()).toString(),
}
this.refreshScreen = this.refreshScreen.bind(this)
}
refreshScreen() {
this.setState({ lastRefresh: Date(Date.now()).toString() })
}
render() {
return (
<View>
<Text>My Screen</Text>
<Text>Last Refresh: {this.state.lastRefresh}</Text>
<Button onPress={this.refreshScreen} title="Refresh Screen" />
</View>
)
}
}
// also put in main View Date(Date.now())
这篇关于React Native重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!