mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-12 22:51:18 +08:00
fix: fix getId being called for incorrect routes. closes #9343
This commit is contained in:
@@ -263,7 +263,9 @@ export default function StackRouter(options: StackRouterOptions) {
|
||||
|
||||
const route = id
|
||||
? state.routes.find(
|
||||
(route) => id === getId?.({ params: route.params })
|
||||
(route) =>
|
||||
route.name === action.payload.name &&
|
||||
id === getId?.({ params: route.params })
|
||||
)
|
||||
: undefined;
|
||||
|
||||
@@ -361,7 +363,9 @@ export default function StackRouter(options: StackRouterOptions) {
|
||||
|
||||
if (id) {
|
||||
index = state.routes.findIndex(
|
||||
(route) => id === getId?.({ params: route.params })
|
||||
(route) =>
|
||||
route.name === action.payload.name &&
|
||||
id === getId?.({ params: route.params })
|
||||
);
|
||||
} else if (
|
||||
(state.routes[state.index].name === action.payload.name &&
|
||||
|
||||
@@ -616,6 +616,48 @@ it('ensures unique ID for navigate', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('ensure unique ID is only per route name for navigate', () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {
|
||||
baz: ({ params }) => params?.foo,
|
||||
bar: ({ params }) => params?.foo,
|
||||
qux: ({ params }) => params?.test,
|
||||
},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
|
||||
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
|
||||
],
|
||||
},
|
||||
CommonActions.navigate('bar', { foo: 'a' }),
|
||||
options
|
||||
)
|
||||
).toEqual({
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 2,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
|
||||
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
|
||||
{ key: 'bar-test', name: 'bar', params: { foo: 'a' } },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('handles go back action', () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
@@ -1215,6 +1257,48 @@ it('ensures unique ID for push', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('ensure unique ID is only per route name for push', () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {
|
||||
baz: ({ params }) => params?.foo,
|
||||
bar: ({ params }) => params?.foo,
|
||||
qux: ({ params }) => params?.test,
|
||||
},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
|
||||
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
|
||||
],
|
||||
},
|
||||
StackActions.push('bar', { foo: 'a' }),
|
||||
options
|
||||
)
|
||||
).toEqual({
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 2,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
|
||||
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
|
||||
{ key: 'bar-test', name: 'bar', params: { foo: 'a' } },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("doesn't merge params on navigate to an existing screen", () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
|
||||
Reference in New Issue
Block a user