You could handle all your routing like that, but...wait a minute...if you turn a url sideways, it kinda looks like a stack yeah?
/users/1/profile users------- 1-------profile
react-stack-nav runs with this idea and introduces the idea of an orchestrator. Orchestrators pops off an element in the stack and declaratively handles transitions when it changes:
users -------> Popped off and handled by first orchestrator in component tree------- 1 -------> Popped off and handled by second orchestrator in component tree-------profile -------> Popped off and handled by third orchestrator in component tree
The first orchestrator found in the component tree would handle changes to the top of the stack:
The next orchestrators found in the tree would handle the next layer of the url stack, so UsersIndex might look like this:
class UsersIndex extends Component { async componentWillReceiveProps(next) { const { routeFragment } = this.props if (routeFragment !== next.routeFragment) return this.setState({ user: await fetchUserData(routeFragment) }) } render() { <View> {/* Use this.state.user info */} </View> }}export default createOrchestrator('users')(UsersIndex)
createOrchestrator() accepts a string or a regular expression for that particular layer of that stack. If it doesn't match the current layer of the url, props.routeFragment will be undefined.
You could create orchestrators ad inifitum all the way down the component tree to handle as much of the URL stack as you want:
pushState(stateObj: object, title: string, url: string): Pushes a new state onto the history array
replaceState(stateObj: object, title: string, url: string): Replaces the current state on the history array
back(reduxOnly: bool): Moves the navigation.index back one. If reduxOnly is true, it won't change the browser history state (this is mostly for internal use)
forward(reduxOnly: bool): Moves the navigation.index forward one, reduxOnly is the same as for back()
go(numberOfEntries: int): Moves the navigation.index forward/backward by the passed number (go(1) is the same as forward(), for example).
replaceTop(stateObj: object, title: string, toUrl: string): Like replaceState, but appends the toUrl to the current url instead of replacing it outright. Primarily used for index redirects.
pushTop(stateObj: object, title: string, toUrl: string): Like pushState, but appends the toUrl to the current url instead of replacing it outright. Primarily used for card stacks.