Skip to content

Instantly share code, notes, and snippets.

@WaldoJeffers
Created August 24, 2018 08:42
Show Gist options
  • Save WaldoJeffers/7826b3a6a4e3185d6feeeac85dbb6ad9 to your computer and use it in GitHub Desktop.
Save WaldoJeffers/7826b3a6a4e3185d6feeeac85dbb6ad9 to your computer and use it in GitHub Desktop.
Dynamic initialRouteName for React Navigation
import { renderNothing, lifecycle } from 'recompose';
/**
* HOC to navigate to a dynamically computed route name
* React Navigation does currently not accept a function to determine
* the initial route at runtime
*/
const withDynamicInitialRouteName = Navigator => (screens, config = {}) => {
if (typeof config.initialRouteName === 'function') {
const getInitialRouteName = config.initialRouteName;
// eslint-disable-next-line no-param-reassign
screens.initialRouteController = lifecycle({
async componentDidMount() {
this.props.navigation.replace(getInitialRouteName(this.props));
},
})(renderNothing());
// eslint-disable-next-line no-param-reassign
config.initialRouteName = 'initialRouteController';
}
return Navigator(screens, config);
};
export default withDynamicInitialRouteName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment