Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: don't merge state with existing state during reset. fixes #111
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Sep 28, 2019
1 parent 9810a06 commit 7393464
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
11 changes: 3 additions & 8 deletions packages/core/src/BaseRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import shortid from 'shortid';
import { CommonAction, NavigationState } from './types';
import { CommonAction, NavigationState, PartialState } from './types';

/**
* Base router object that can be used when writing custom routers.
Expand All @@ -9,7 +9,7 @@ const BaseRouter = {
getStateForAction<State extends NavigationState>(
state: State,
action: CommonAction
): State | null {
): State | PartialState<State> | null {
switch (action.type) {
case 'REPLACE': {
const index = action.source
Expand Down Expand Up @@ -56,12 +56,7 @@ const BaseRouter = {
}

case 'RESET':
return {
...state,
...action.payload,
key: state.key,
routeNames: state.routeNames,
};
return action.payload as PartialState<State>;

default:
return null;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/NavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const MISSING_CONTEXT_ERROR =
export const NavigationStateContext = React.createContext<{
state?: NavigationState | PartialState<NavigationState>;
getState: () => NavigationState | PartialState<NavigationState> | undefined;
setState: (state: NavigationState | undefined) => void;
setState: (
state: NavigationState | PartialState<NavigationState> | undefined
) => void;
key?: string;
performTransaction: (action: () => void) => void;
}>({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/SceneView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function SceneView<
}, [getState, route.key]);

const setCurrentState = React.useCallback(
(child: NavigationState | undefined) => {
(child: NavigationState | PartialState<NavigationState> | undefined) => {
const state = getState();

setState({
Expand Down
12 changes: 1 addition & 11 deletions packages/core/src/__tests__/BaseRouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,5 @@ it('resets state to new state with RESET', () => {
})
);

expect(result).toEqual({ ...STATE, index: 0, routes });
});

it('ignores key and routeNames when resetting with RESET', () => {
const result = BaseRouter.getStateForAction(
STATE,
// @ts-ignore
CommonActions.reset({ index: 2, key: 'foo', routeNames: ['test'] })
);

expect(result).toEqual({ ...STATE, index: 2 });
expect(result).toEqual({ index: 0, routes });
});
5 changes: 4 additions & 1 deletion packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export type Router<
* @param state State object to apply the action on.
* @param action Action object to apply.
*/
getStateForAction(state: State, action: Action): State | null;
getStateForAction(
state: State,
action: Action
): State | PartialState<State> | null;

/**
* Whether the action should also change focus in parent navigator
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/useOnAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import * as React from 'react';
import NavigationBuilderContext, {
ChildActionListener,
} from './NavigationBuilderContext';
import { NavigationAction, NavigationState, Router } from './types';
import {
NavigationAction,
NavigationState,
PartialState,
Router,
} from './types';

type Options = {
router: Router<NavigationState, NavigationAction>;
key?: string;
getState: () => NavigationState;
setState: (state: NavigationState) => void;
setState: (state: NavigationState | PartialState<NavigationState>) => void;
listeners: ChildActionListener[];
};

Expand Down

0 comments on commit 7393464

Please sign in to comment.