Created
August 24, 2018 08:42
-
-
Save WaldoJeffers/7826b3a6a4e3185d6feeeac85dbb6ad9 to your computer and use it in GitHub Desktop.
Dynamic initialRouteName for React Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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