Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goBack() additional functionality #905

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/guides/Screen-Navigation-Prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class ProfileScreen extends React.Component {
## `goBack` - Close the active screen and move back

Optionally provide a key, which specifies the route to go back from. By default, goBack will close the route that it is called from. If the goal is to go back *anywhere*, without specifying what is getting closed, call `.goBack(null);`
Accepts such objects:
`.goBack({ key: 'your-route-key' })`
`.goBack({ numberOfPages: 3 })` - goes back `numberOfPages` screens
`.goBack({ routeName: 'YourRouteName' })` - goes back to the screen in stack with such `routeName`.

```js
class HomeScreen extends React.Component {
Expand All @@ -107,7 +111,7 @@ class HomeScreen extends React.Component {
title="Go back anywhere"
/>
<Button
onPress={() => goBack('screen-123')}
onPress={() => goBack({key: 'screen-123'})}
title="Go back from screen-123"
/>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/addNavigationHelpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('addNavigationHelpers', () => {
expect(addNavigationHelpers({
state: { key: 'A', routeName: 'Home' },
dispatch: mockedDispatch,
}).goBack('A')).toEqual(true);
}).goBack({ key: 'A' })).toEqual(true);
expect(mockedDispatch).toBeCalledWith({ type: NavigationActions.BACK, key: 'A' });
expect(mockedDispatch.mock.calls.length).toBe(1);
});
Expand Down
4 changes: 1 addition & 3 deletions src/addNavigationHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import NavigationActions from './NavigationActions';
export default function<S: *> (navigation: NavigationProp<S, NavigationAction>) {
return {
...navigation,
goBack: (key?: ?string): boolean => navigation.dispatch(NavigationActions.back({
key: key === undefined ? navigation.state.key : key,
})),
goBack: (params?: ?object): boolean => navigation.dispatch(NavigationActions.back(params)),
navigate: (
routeName: string,
params?: NavigationParams,
Expand Down
18 changes: 13 additions & 5 deletions src/routers/StackRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export default (

getStateForAction(action: NavigationStackAction, state: ?NavigationState) {
action = NavigationActions.mapDeprecatedActionAndWarn(action);

// Set up the initial state if needed
if (!state) {
let route = {};
Expand Down Expand Up @@ -210,20 +209,29 @@ export default (

if (action.type === NavigationActions.BACK) {
let backRouteIndex = null;

if (action.routeName) {
backRouteIndex = state.routes.findIndex((route: *) => route.routeName === action.routeName);
}

if (action.numberOfPages) {
backRouteIndex = state.index - action.numberOfPages;
}

if (action.key) {
/* $FlowFixMe */
const backRoute = state.routes.find((route: *) => route.key === action.key);
/* $FlowFixMe */
backRouteIndex = state.routes.indexOf(backRoute);
backRouteIndex = state.routes.indexOf(backRoute) - 1;
}
if (backRouteIndex == null) {
return StateUtils.pop(state);
}
if (backRouteIndex > 0) {
if (backRouteIndex > -1) {
return {
...state,
routes: state.routes.slice(0, backRouteIndex),
index: backRouteIndex - 1,
routes: state.routes.slice(0, backRouteIndex + 1),
index: backRouteIndex,
};
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/views/ScenesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default function ScenesReducer(
const staleScenes: Map<string, NavigationScene> = new Map();

// Populate stale scenes from previous scenes marked as stale.

scenes.forEach((scene) => {
const { key } = scene;
if (scene.isStale) {
Expand Down Expand Up @@ -157,7 +158,21 @@ export default function ScenesReducer(
}
});

staleScenes.forEach(mergeScene);

// work around for flashing scenes
let k = null;
let v = null;
staleScenes.forEach(scene => {
let { key } = scene;
k = key;
v = scene;
});

newStaleScenes = k && v ? new Map([[k, v]]) : new Map();
newStaleScenes.forEach(mergeScene);
// staleScenes.forEach(mergeScene);
// work around end

freshScenes.forEach(mergeScene);

nextScenes.sort(compareScenes);
Expand Down