From 5fff7ef5c64f4af0595b97a173d1c083b56de39c Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Thu, 26 Apr 2018 00:48:55 +0000 Subject: [PATCH] Give inactive routes in stack opportunity to handle action (#4064) --- examples/NavigationPlayground/js/App.js | 8 ++ .../NavigationPlayground/js/InactiveStack.js | 96 +++++++++++++++++++ src/StateUtils.js | 23 ++++- src/__tests__/NavigationStateUtils-test.js | 4 +- .../__snapshots__/TabNavigator-test.js.snap | 4 +- src/routers/StackRouter.js | 31 +++++- src/routers/__tests__/StackRouter-test.js | 71 ++++++++++++-- 7 files changed, 219 insertions(+), 18 deletions(-) create mode 100644 examples/NavigationPlayground/js/InactiveStack.js diff --git a/examples/NavigationPlayground/js/App.js b/examples/NavigationPlayground/js/App.js index c091ca6..9db6a2c 100644 --- a/examples/NavigationPlayground/js/App.js +++ b/examples/NavigationPlayground/js/App.js @@ -27,6 +27,7 @@ import ModalStack from './ModalStack'; import StacksInTabs from './StacksInTabs'; import StacksOverTabs from './StacksOverTabs'; import StacksWithKeys from './StacksWithKeys'; +import InactiveStack from './InactiveStack'; import StackWithCustomHeaderBackImage from './StackWithCustomHeaderBackImage'; import SimpleStack from './SimpleStack'; import StackWithHeaderPreset from './StackWithHeaderPreset'; @@ -45,6 +46,11 @@ const ExampleInfo = { name: 'Switch between routes', description: 'Jump between routes', }, + InactiveStack: { + name: 'Navigate idempotently to stacks in inactive routes', + description: + 'An inactive route in a stack should be given the opportunity to handle actions', + }, StackWithCustomHeaderBackImage: { name: 'Custom header back image', description: 'Stack with custom header back image', @@ -150,6 +156,8 @@ const ExampleRoutes = { }, TabsWithNavigationFocus, KeyboardHandlingExample, + // This is commented out because it's rarely useful + // InactiveStack, }; type State = { diff --git a/examples/NavigationPlayground/js/InactiveStack.js b/examples/NavigationPlayground/js/InactiveStack.js new file mode 100644 index 0000000..17b28ea --- /dev/null +++ b/examples/NavigationPlayground/js/InactiveStack.js @@ -0,0 +1,96 @@ +import React from 'react'; +import { Button, Text, StatusBar, View, StyleSheet } from 'react-native'; +import { + SafeAreaView, + createStackNavigator, + createSwitchNavigator, + NavigationActions, +} from 'react-navigation'; + +const runSubRoutes = navigation => { + navigation.dispatch(NavigationActions.navigate({ routeName: 'First2' })); + navigation.dispatch(NavigationActions.navigate({ routeName: 'Second2' })); + navigation.dispatch(NavigationActions.navigate({ routeName: 'First2' })); +}; + +const runSubRoutesWithIntermediate = navigation => { + navigation.dispatch(toFirst1); + navigation.dispatch(toSecond2); + navigation.dispatch(toFirst); + navigation.dispatch(toFirst2); +}; + +const runSubAction = navigation => { + navigation.dispatch(toFirst2); + navigation.dispatch(toSecond2); + navigation.dispatch(toFirstChild1); +}; + +const DummyScreen = ({ routeName, navigation, style }) => { + return ( + + + {routeName}({navigation.state.key}) + + +