Skip to content

Instantly share code, notes, and snippets.

@alexrussell
Created November 15, 2017 12:43
Show Gist options
  • Save alexrussell/65aace4c6f9d3bec01876d1083c79705 to your computer and use it in GitHub Desktop.
Save alexrussell/65aace4c6f9d3bec01876d1083c79705 to your computer and use it in GitHub Desktop.
Idea of how I have implemented a `goBackTo` saga for react-navigation.
export const goBackTo = function * goBackTo ({ payload: { routeName } }) {
const { nav } = yield select()
let targetIndex
for (let i = nav.index; i > 0; i--) {
if (nav.routes[i].routeName === routeName) {
targetIndex = i + 1
break
}
}
if (targetIndex) {
// Index found, let's make sure it's within the array (if it's not, it means the current screen
// was the screen found with the route name, and the index is therefore the next index (which
// doesn't exist), and so we literally just need to do nothing).
if (targetIndex < nav.routes.length) {
const key = nav.routes[targetIndex].key
yield put(NavigationActions.back({ key }))
}
} else {
// No index found, so let's just go back one screen
yield put(NavigationActions.back())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment