react-native深层链接参数不会消失

问题描述

我在react-native中使用Depp-Link

https://reactnavigation.org/docs/configuring-links (反应导航v5)

如果我点击

我的配置是

const config = {
  screens: {
    HomeStack: {
      initialRouteName: 'HomeMain',screens: {
        HomeMain: {
          path: '',}
      }
    },ProductStack: {
      initialRouteName: 'ProductMain',screens: {
        Ranking: 'ranking/:category?/:order?',}
    },// NotFound: '*',}
}
const linking: LinkingOptions = {
  prefixes: ['myapp://'],config,};

排名屏幕是

const Ranking = ({ route }: Props) => {
  console.log('?? route ??',route.params);

  return (
    <View>
      ...
    </View>
  );
};

当我在Safari(iOS模拟器)中编写“ myapp:// ranking?category = aa&order = bb”时, 可以。

但是我在没有DeepLink的情况下输入“排名”,route.params并没有消失并保持不变。 我只想通过深层链接应用它。

我该怎么办?

解决方法

u可以在组件中使用Linking-native addEventlistener链接,并检查是否在componentDidMount的深层链接中将其打开

const Ranking = ({ route }: Props) => {
 componentDidMount(){
        Linking.addEventListener('url',this.handledeeplink);
}

handledeeplink = async (event) => {
        let url = new URL(event.url);
         console.log(url );// in this u will get url here u can get params        
    }
    
  return (
    <View>
      ...
    </View>
  );
};