反应导航与多个路径的深层链接

Max*_*hev 3 reactjs react-native react-navigation

有什么方法可以配置,react-navigation以便单个屏幕可以处理多个链接?

每个屏幕都StackNavigator可以具有path启用深层链接的可选属性,StackNavigator还可以接受paths允许您覆盖每个特定屏幕的路径的选项,但它仍是一对一的映射。有没有办法声明应由单个屏幕处理的无限数量的路径?

ben*_*nel 7

您可以将变量用于无限数量的路径,如文档中所示StackNavigator

docs中的示例

StackNavigator({
  // For each screen that you can navigate to, create a new entry like this:
  Profile: {
    // `ProfileScreen` is a React component that will be the main content of the screen.
    screen: ProfileScreen,
    // When `ProfileScreen` is loaded by the StackNavigator, it will be given a `navigation` prop.

    // Optional: When deep linking or using react-navigation in a web app, this path is used:
    path: 'people/:name',
    // The action and route params are extracted from the path.

    // Optional: Override the `navigationOptions` for the screen
    navigationOptions: ({ navigation }) => ({
      title: `${navigation.state.params.name}'s Profile'`,
    }),
  },

  ...MyOtherRoutes,
});
Run Code Online (Sandbox Code Playgroud)

更新资料

您可以创建自定义路由处理程序,以更详细地控制此处显示的路径。

docs中的示例

import { NavigationActions } from 'react-navigation'

const MyApp = StackNavigator({
  Home: { screen: HomeScreen },
  Profile: { screen: ProfileScreen },
}, {
  initialRouteName: 'Home',
})
const previousGetActionForPathAndParams = MyApp.router.getActionForPathAndParams;

Object.assign(MyApp.router, {
  getActionForPathAndParams(path, params) {
    if (
      path === 'my/custom/path' &&
      params.magic === 'yes'
    ) {
      // returns a profile navigate action for /my/custom/path?magic=yes
      return NavigationActions.navigate({
        routeName: 'Profile',
        action: NavigationActions.navigate({
          // This child action will get passed to the child router
          // ProfileScreen.router.getStateForAction to get the child
          // navigation state.
          routeName: 'Friends',
        }),
      });
    }
    return previousGetActionForPathAndParams(path, params);
  },
});
Run Code Online (Sandbox Code Playgroud)