从深层链接打开时如何刷新应用程序?
How to refresh app when opened from deeplink?
我正在努力使用 React Native 应用程序。我会实现 react native firebase dynamic link,但现在我有点迷茫了。我在 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 {
}
}
但是当应用程序已经打开时,此方法无法正常工作。有什么方法可以让我在每次有人回到打开的应用程序时触发一个功能吗?
提示,您应该将代码放在 componentDidMount
中,这样您就不会阻止初始(第一个)渲染。
您可以使用 AppState
来监听 background/foreground 中应用的变化。
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);
}
}
我正在努力使用 React Native 应用程序。我会实现 react native firebase dynamic link,但现在我有点迷茫了。我在 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 {
}
}
但是当应用程序已经打开时,此方法无法正常工作。有什么方法可以让我在每次有人回到打开的应用程序时触发一个功能吗?
提示,您应该将代码放在 componentDidMount
中,这样您就不会阻止初始(第一个)渲染。
您可以使用 AppState
来监听 background/foreground 中应用的变化。
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);
}
}