簡體   English   中英

從Deeplink打開時如何刷新應用程序?

[英]How to refresh app when opened from deeplink?

我正在努力使用本機應用程序。 我會實現React Native Firebase動態鏈接,但是現在我有點迷路了。 我在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偵聽在后台/前景中放置的應用程序的更改。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM