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

时间:2020-08-29 06:41:27

标签: react-native react-navigation url-scheme

我在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并没有消失并保持不变。 我只想通过深层链接应用它。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

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>
  );
};