Skip to content

Commit

Permalink
Add SetParams handling in TabRouter.js (react-navigation#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
microwavesafe authored and sourcecode911 committed Mar 9, 2020
1 parent f64e222 commit 5cecc44
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/routers/TabRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ export default (
}
// console.log({navId: order.join('-'), action, order, lastIndex: state.index, index: activeTabIndex});
}
if (action.type === NavigationActions.SET_PARAMS) {
const lastRoute = state.routes.find(route => route.key === action.key);
if (lastRoute) {
const params = {
...lastRoute.params,
...action.params,
};
const routes = [...state.routes];
routes[state.routes.indexOf(lastRoute)] = {
...lastRoute,
params,
};
return {
...state,
routes,
};
}
}
if (activeTabIndex !== state.index) {
// console.log(`${order.join('-')}: Normal navigation`, {lastIndex: state.index, newIndex: activeTabIndex});
return {
Expand Down
17 changes: 17 additions & 0 deletions src/routers/__tests__/TabRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ describe('TabRouter', () => {
});
});

test('Handles the SetParams action', () => {
const router = TabRouter({
Foo: {
screen: () => <div />,
},
Bar: {
screen: () => <div />,
},
});
const state2 = router.getStateForAction({
type: NavigationActions.SET_PARAMS,
params: { name: 'Qux' },
key: 'Foo',
});
expect(state2 && state2.routes[0].params).toEqual({ name: 'Qux' });
});

test('getStateForAction returns null when navigating to same tab', () => {
const router = TabRouter({ Foo: BareLeafRouteConfig, Bar: BareLeafRouteConfig }, { initialRouteName: 'Bar' });
const state = router.getStateForAction({ type: NavigationActions.INIT });
Expand Down

0 comments on commit 5cecc44

Please sign in to comment.