【问题标题】:React Navigation get Param from deep linking not workingReact Navigation 从深度链接中获取参数不起作用
【发布时间】:2019-09-13 15:35:11
【问题描述】:

我的路由器配置如下:

const SwitchRouter = createSwitchNavigator(
  {
    Splash: {
      screen: Launch,
      path: 'hello/:code',
    },
    App: HomeStack,
  },
  {
    initialRouteName: 'Splash',
  }
);

我在 Safari 中使用一个链接,它启动我的 iOS 应用程序,然后我应该从我的代码中的这个链接获取一个参数。

我尝试了不同的链接,但我无法从中获取任何参数。这是我尝试过的:

  • myApp://hello/123
  • myApp://hello/?code=123
  • myApp://hello?code=123

我的代码应该得到这个code parementer 在我的启动屏幕中,如下所示:

const code = navigation.getParam('code', 'error');

代码值总是错误的,我这里的参数永远找不到。 我在这里错过了什么吗?我浏览了所有 GitHub 和 react-navigation 文档,但找不到适合我的解决方案。

我读到有些人在 componentDidMount 中获取深层链接参数时遇到问题。显然它们不可用。 所以我的代码在这里负责获取我的参数“代码”,我尝试在 componentDidMount/DidUpdate 甚至在渲染中使用它,但在所有情况下我都无法获取我的参数。

【问题讨论】:

    标签: ios react-native react-navigation deep-linking


    【解决方案1】:

    我认为您不能将深层链接参数传递到第一个/初始屏幕。 您必须改用代理:

    const SwitchRouter = createSwitchNavigator(
    {
        CodeScreen: {
           Screen: Code,
           path: 'code/:code',
        }
        Splash: {
           screen: Launch,
           path: 'hello',
        },
        App: HomeStack,
       },
       {
         initialRouteName: 'Splash',
       }
    });
    
    
    
    class Code extends Component {
    
    constructor(props) {
        super(props);
    
        const {navigation} = this.props;
        const code = navigation.getParam('code', false);
    
        navigation.navigate('hello', {code});
    
    }
    
    render() {
        return (
            <View>
    
            </View>
        );
    }
    

    }

    现在您在 SplashScreen 中检索“代码”参数。

    【讨论】:

    • 当我的应用处于后台状态时,我没有得到参数,你知道为什么吗?
    猜你喜欢
    • 2021-08-10
    • 2019-08-27
    • 1970-01-01
    • 2017-09-18
    • 2023-02-25
    • 2023-02-18
    • 2019-02-16
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多