【问题标题】:react-native deeplink only works one timereact-native deeplink 只工作一次
【发布时间】:2019-10-30 14:47:53
【问题描述】:

我在 react-native 中使用深度链接,以便在用户点击按钮时将用户重定向到特定的 youtube 频道。

它运行良好,但是当用户使用后退按钮返回应用程序时,它显示一个空白屏幕,我无法再将用户重定向到 youtube 频道。

我怎样才能使深度链接每次都能正常工作?谢谢 !

import React from 'react';
import { View, Text, Linking, Platform } from 'react-native';

class ChaineYT extends React.Component {
  state = {
    isLoading:false,
    isLinked: false
  }

  componentDidMount = () => {
    Linking.openURL('http://www.youtube.com/channel/UC1UpcbyFVTZTDrvdjNmSzSg');
      this.setState({isLoading:true, isLinked:true});
        if(this.state.isLoading && this.state.isLinked){
        this.props.navigation.navigate('Acceuil')
      }
  }

  render() {
    return (
      <View>

      </View>
    )
  }
}

export default ChaineYT

如何在后台管理 youtube 应用?

【问题讨论】:

  • 我认为您在if(this.state.isLoading &amp;&amp; this.state.isLinking) 行中输入错误this.state.isLinked
  • 不,这是我的错误,但我的代码中有this.state.isLinked

标签: react-native deep-linking


【解决方案1】:

您将不得不为此使用 AppState

import React from 'react';
import { View, Text, Linking, Platform, AppState } from 'react-native';

class ChaineYT extends React.Component {
  state = {
    isLoading:false,
    isLinked: false,
    appState: AppState.currentState,

  }

  componentDidMount = () => {
    Linking.openURL('http://www.youtube.com/channel/UC1UpcbyFVTZTDrvdjNmSzSg');
      this.setState({isLoading:true, isLinked:true});

    AppState.addEventListener('change', this._handleAppStateChange);

  }

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

  _handleAppStateChange = (nextAppState) => {
    if (
      this.state.appState.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {
       if(this.state.isLoading && this.state. isLinked){
        this.props.navigation.navigate('Acceuil')
      }
    }
    this.setState({appState: nextAppState});
  };

  render() {
    return (
      <View>

      </View>
    )
  }
}

export default ChaineYT

【讨论】:

  • 感谢您的回答!但我试过了,结果是一样的,当我点击抽屉里的Youtube Channel按钮时,它会启动youtube应用程序,但如果我回来,它仍然不会在我点击Youtube Channel时重新渲染youtube频道跨度>
【解决方案2】:

从头开始解释 react-native 中带有 react-navigation v5 的 Deeplink here

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多