react-navigation / react-navigation.github.io

Home of the documentation and other miscellanea
https://reactnavigation.org/
MIT License
310 stars 1.87k forks source link

Navigate by path documentation #25

Open brentvatne opened 6 years ago

brentvatne commented 6 years ago

Is there a public API for jumping to some path, like what happens when the app is opened by a deep link? would it just be Linking.openURL? https://github.com/react-navigation/react-navigation/blob/c72b44ce104e5fbeb67624c44b792eca82285119/src/createNavigationContainer.js#L75-L84

s-nel commented 5 years ago

The problem with using Linking.openURL on Android is it'll ask you which app you want to open with. We should be able to navigate to a path without potentially kicking the user into a different app or causing confusion. Take this use case for example:

  1. User installs app
  2. User clicks dynamic link https://example.page.link
  3. Android prompts user to open links for https://example.page.link with application
  4. User chooses to open link with application
  5. Application receives link unwraps it and now needs to navigate to the deep link https://example.com/xxx
  6. Uses Linking.openURL('https://example.com/xxx')
  7. Android prompts user to open links for https://example.com with the application
  8. User is very confused why they were asked twice to use application to open a single link

I think react-navigation should have a public API for navigating to a path

shawnthye commented 4 years ago

The problem with using Linking.openURL on Android is it'll ask you which app you want to open with. We should be able to navigate to a path without potentially kicking the user into a different app or causing confusion. Take this use case for example:

  1. User installs app
  2. User clicks dynamic link https://example.page.link
  3. Android prompts user to open links for https://example.page.link with application
  4. User chooses to open link with application
  5. Application receives link unwraps it and now needs to navigate to the deep link https://example.com/xxx
  6. Uses Linking.openURL('https://example.com/xxx')
  7. Android prompts user to open links for https://example.com with the application
  8. User is very confused why they were asked twice to use application to open a single link

I think react-navigation should have a public API for navigating to a path

I think we can use applink For example yourappname://items/123

So it won't ask user for choosing apps.

But Android App Links on Android 6.0 (API level 23)

bramski commented 4 years ago

Indeed. What's the status here? We are using Branch.io for our links and I just finished upgrading to react-navigation 3.0. I'd like to switch to using the path based navigation for the branch links but this is not well documented. Can I just get a ref to the appContainer and ask it to navigate for me?

turingpavan commented 4 years ago
import { NavigationActions } from 'react-navigation';

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigate(routeName, params) {

  try {
    _navigator.dispatch(
      NavigationActions.navigate({
        routeName,
        params,
      })
    );
  } catch (error) {
    console.log(error);
  }

}

function navigateToPath(path, params) {

  const action = _navigator
    && _navigator._navigation
    && _navigator._navigation.router
    && _navigator._navigation.router.getActionForPathAndParams(path);

  if (action) {
    if (action.action) {
      if (action.action.action) {
        _navigator.dispatch(action.action.action);
      } else {
        _navigator.dispatch(action.action);
      }
    } else {
      _navigator.dispatch(action);
    }
  }
}

// add other navigation functions that you need and export them

export default {
  navigate,
  setTopLevelNavigator,
  navigateToPath
};

navigateToPath('/profile') will take to Screen corresponding to path: 'profile'