mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 17:34:59 +08:00
feat: add ability to dispatch a update function
This commit is contained in:
@@ -99,9 +99,12 @@ export type NavigationHelpers<
|
||||
ParamList extends ParamListBase = ParamListBase
|
||||
> = {
|
||||
/**
|
||||
* Dispatch an action to the router.
|
||||
* Dispatch an action or an update function to the router.
|
||||
* The update function will receive the current state,
|
||||
*/
|
||||
dispatch(action: NavigationAction): void;
|
||||
dispatch(
|
||||
action: NavigationAction | ((state: NavigationState) => NavigationState)
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Navigate to a route in current navigation tree.
|
||||
|
||||
@@ -180,8 +180,12 @@ export default function useNavigationBuilder(
|
||||
);
|
||||
|
||||
const helpers = React.useMemo((): NavigationHelpers => {
|
||||
const dispatch = (action: NavigationAction) => {
|
||||
if (!onAction(action)) {
|
||||
const dispatch = (
|
||||
action: NavigationAction | ((state: NavigationState) => NavigationState)
|
||||
) => {
|
||||
if (typeof action === 'function') {
|
||||
setState(action(getState()));
|
||||
} else if (!onAction(action)) {
|
||||
throw new Error(
|
||||
`No navigators are able to handle the action "${action.type}".`
|
||||
);
|
||||
@@ -206,7 +210,7 @@ export default function useNavigationBuilder(
|
||||
),
|
||||
dispatch,
|
||||
};
|
||||
}, [onAction, parentNavigationHelpers, router.actionCreators]);
|
||||
}, [getState, onAction, parentNavigationHelpers, router, setState]);
|
||||
|
||||
const navigation = React.useMemo(
|
||||
() => ({
|
||||
|
||||
Reference in New Issue
Block a user