[英]How to refresh app when opened from deeplink?
I am struggling with a react native app. 我正在努力使用本机应用程序。 I would implement react native firebase dynamic link, but now I am a little lost.
我会实现React Native Firebase动态链接,但是现在我有点迷路了。 I use this method on HomeScreen which working perfectly every times when somebody opens the app.
我在HomeScreen上使用此方法,每次有人打开应用程序时,它都能完美运行。
async componentWillMount() {
try {
let url = await firebase.links().getInitialLink();
if(url) {
let api = "example.com/user/123456";
try {
this.setState({ data: "John Doe" });
this.props.navigation.navigate('Preview', {user: this.state.data })
}
catch {
}
}
}
catch {
}
}
But when the app is already opened this method doesn't work properly. 但是,当应用程序已经打开时,此方法将无法正常工作。 Is there a way where I can trigger a function every time when somebody comes back to the opened app?
有没有一种方法可以在有人回到打开的应用程序时每次触发功能?
Just a tip, you should place your code in componentDidMount
so that you do not block the initial (first) render. 提示,您应该将代码放置在
componentDidMount
以免阻塞初始(第一个)渲染。
You could use AppState
to listen out for changes to apps being put in the background/foreground. 您可以使用
AppState
侦听在后台/前景中放置的应用程序的更改。
componentDidMount() {
this.showPreview();
AppState.addEventListener('change', this.onAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.onAppStateChange);
}
const onAppStateChange = appState => {
// You can check if appState is active/background/foreground
this.showPreview();
}
const showPreview = async (appState) => {
// You can check if appState is active/inactive/background
try {
let url = await firebase.links().getInitialLink();
if(url) {
let api = "example.com/user/123456";
try {
this.setState({ data: "John Doe" });
this.props.navigation.navigate('Preview', {user: this.state.data })
}
catch {
}
}
}
catch(e) {
console.error(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.