NavigationUtil.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. |--------------------------------------------------
  3. | Navigator Utils
  4. |--------------------------------------------------
  5. */
  6. import { NavigationActions } from 'react-navigation'
  7. const reset = (navigation, routeName, params, index, cb) => {
  8. const resetAction = NavigationActions.reset({
  9. index: index || 0,
  10. actions: [NavigationActions.navigate({ routeName, params })],
  11. })
  12. navigation.dispatch(resetAction)
  13. cb && cb()
  14. }
  15. const resets = (navigation, routeNames, index) => {
  16. let _action = {
  17. index: index,
  18. actions: [],
  19. }
  20. routeNames.forEach(routeName =>
  21. _action.actions.push(NavigationActions.navigate({ routeName }))
  22. )
  23. const resetAction = NavigationActions.reset(_action)
  24. navigation.dispatch(resetAction)
  25. }
  26. const back = (navigation, key) => {
  27. const backAction = NavigationActions.back({
  28. key: key,
  29. })
  30. navigation.dispatch(backAction)
  31. }
  32. const action = (navigation, routeName, params) => {
  33. const navigationAction = NavigationActions.navigate({
  34. routeName: routeName,
  35. params: params || {},
  36. action: NavigationActions.navigate({ routeName }),
  37. })
  38. navigation.dispatch(navigationAction)
  39. }
  40. const setParams = (navigation, key, params) => {
  41. const setParamsAction = NavigationActions.setParams({
  42. // these are the new params that will be merged into the existing route params
  43. params: params || {},
  44. // The key of the route that should get the new params
  45. key: key,
  46. })
  47. navigation.dispatch(setParamsAction)
  48. }
  49. export default {
  50. reset,
  51. back,
  52. action,
  53. setParams,
  54. resets,
  55. }