mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
refactor: remove key argument from setParams
We now have a source property in the action which does the same as what the key argument did.
This commit is contained in:
@@ -28,7 +28,7 @@ export type Action =
|
||||
}
|
||||
| {
|
||||
type: 'SET_PARAMS';
|
||||
payload: { key?: string; params?: object };
|
||||
payload: { params?: object };
|
||||
source?: string;
|
||||
target?: string;
|
||||
};
|
||||
@@ -63,6 +63,6 @@ export function reset(
|
||||
return { type: 'RESET', payload: state };
|
||||
}
|
||||
|
||||
export function setParams(params: object, key: string): Action {
|
||||
return { type: 'SET_PARAMS', payload: { key, params } };
|
||||
export function setParams(params: object): Action {
|
||||
return { type: 'SET_PARAMS', payload: { params } };
|
||||
}
|
||||
|
||||
@@ -35,12 +35,9 @@ const BaseRouter = {
|
||||
}
|
||||
|
||||
case 'SET_PARAMS': {
|
||||
const index =
|
||||
action.payload.key || action.source
|
||||
? state.routes.findIndex(
|
||||
r => r.key === action.payload.key || r.key === action.source
|
||||
)
|
||||
: state.index;
|
||||
const index = action.source
|
||||
? state.routes.findIndex(r => r.key === action.source)
|
||||
: state.index;
|
||||
|
||||
if (index === -1) {
|
||||
return null;
|
||||
|
||||
@@ -413,59 +413,6 @@ it('updates route params with setParams', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('updates another route params with setParams', () => {
|
||||
const TestNavigator = (props: any) => {
|
||||
const { state, descriptors } = useNavigationBuilder(MockRouter, props);
|
||||
|
||||
return descriptors[state.routes[state.index].key].render();
|
||||
};
|
||||
|
||||
let setParams: (params: object, key: string) => void = () => undefined;
|
||||
|
||||
const FooScreen = (props: any) => {
|
||||
setParams = props.navigation.setParams;
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const onStateChange = jest.fn();
|
||||
|
||||
render(
|
||||
<NavigationContainer onStateChange={onStateChange}>
|
||||
<TestNavigator initialRouteName="foo">
|
||||
<Screen name="foo" component={FooScreen} />
|
||||
<Screen name="bar" component={jest.fn()} />
|
||||
</TestNavigator>
|
||||
</NavigationContainer>
|
||||
);
|
||||
|
||||
act(() => setParams({ username: 'alice' }, 'bar'));
|
||||
|
||||
expect(onStateChange).toBeCalledTimes(1);
|
||||
expect(onStateChange).lastCalledWith({
|
||||
index: 0,
|
||||
key: '0',
|
||||
routeNames: ['foo', 'bar'],
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo', params: undefined },
|
||||
{ key: 'bar', name: 'bar', params: { username: 'alice' } },
|
||||
],
|
||||
});
|
||||
|
||||
act(() => setParams({ age: 25 }, 'bar'));
|
||||
|
||||
expect(onStateChange).toBeCalledTimes(2);
|
||||
expect(onStateChange).lastCalledWith({
|
||||
index: 0,
|
||||
key: '0',
|
||||
routeNames: ['foo', 'bar'],
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo', params: undefined },
|
||||
{ key: 'bar', name: 'bar', params: { username: 'alice', age: 25 } },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('handles change in route names', () => {
|
||||
const TestNavigator = (props: any): any => {
|
||||
useNavigationBuilder(MockRouter, props);
|
||||
|
||||
@@ -288,11 +288,9 @@ export type NavigationHelpers<
|
||||
* The new params will be shallow merged with the old one.
|
||||
*
|
||||
* @param params Params object for the current route.
|
||||
* @param key Key of the route for updating params.
|
||||
*/
|
||||
setParams<RouteName extends keyof ParamList>(
|
||||
params: ParamList[RouteName],
|
||||
key: string
|
||||
params: ParamList[RouteName]
|
||||
): void;
|
||||
};
|
||||
|
||||
@@ -308,10 +306,8 @@ export type NavigationProp<
|
||||
* The new params will be shallow merged with the old one.
|
||||
*
|
||||
* @param params Params object for the current route.
|
||||
* @param [key] Key of the route for updating params. Defaults to current route.
|
||||
*/
|
||||
setParams(params: ParamList[RouteName]): void;
|
||||
setParams(params: ParamList[keyof ParamList], key: string): void;
|
||||
|
||||
/**
|
||||
* Update the options for the route.
|
||||
|
||||
Reference in New Issue
Block a user