Skip to content

Commit

Permalink
Fix StackRouter Replace Key Behavior (react-navigation#3450)
Browse files Browse the repository at this point in the history
Replace should actually provide new keys on the replaced route, use ‘newKey’ on the action if you want to define the new route key
  • Loading branch information
ericvicenti authored and sourcecode911 committed Mar 9, 2020
1 parent c41157e commit 194fcbf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/NavigationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const reset = createAction(RESET, payload => ({
const replace = createAction(REPLACE, payload => ({
type: REPLACE,
key: payload.key,
newKey: payload.newKey,
params: payload.params,
action: payload.action,
routeName: payload.routeName,
Expand Down
2 changes: 1 addition & 1 deletion src/routers/StackRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default (routeConfigs, stackConfig = {}) => {
params: action.params,
// merge the child state in this order to allow params override
...childState,
key: action.key,
key: action.newKey || generateKey(),
routeName: action.routeName,
};
return { ...state, routes };
Expand Down
13 changes: 13 additions & 0 deletions src/routers/__tests__/StackRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,21 @@ describe('StackRouter', () => {
);
expect(replacedState.index).toEqual(0);
expect(replacedState.routes.length).toEqual(1);
expect(replacedState.routes[0].key).not.toEqual(initState.routes[0].key);
expect(replacedState.routes[0].routeName).toEqual('bar');
expect(replacedState.routes[0].params.meaning).toEqual(42);
const replacedState2 = TestRouter.getStateForAction(
NavigationActions.replace({
routeName: 'bar',
key: initState.routes[0].key,
newKey: 'wow',
}),
initState
);
expect(replacedState2.index).toEqual(0);
expect(replacedState2.routes.length).toEqual(1);
expect(replacedState2.routes[0].key).toEqual('wow');
expect(replacedState2.routes[0].routeName).toEqual('bar');
});

test('Handles push transition logic with completion action', () => {
Expand Down

0 comments on commit 194fcbf

Please sign in to comment.