ReactNativeUtil icon indicating copy to clipboard operation
ReactNativeUtil copied to clipboard

react navigation 快速点击跳转问题

Open wuyunqiang opened this issue 7 years ago • 1 comments

第一种方式修改源码: 修改react-navigation目录下,scr文件夹中的addNavigationHelpers.js文件

export default function<S: *>(navigation: NavigationProp<S, NavigationAction>) {
  // 添加点击判断
  let debounce = true;
  return {
      ...navigation,
      goBack: (key?: ?string): boolean =>
          navigation.dispatch(
              NavigationActions.back({
                  key: key === undefined ? navigation.state.key : key,
              }),
          ),
      navigate: (routeName: string,
                 params?: NavigationParams,
                 action?: NavigationAction,): boolean => {
          if (debounce) {
              debounce = false;
              navigation.dispatch(
                  NavigationActions.navigate({
                      routeName,
                      params,
                      action,
                  }),
              );
              setTimeout(
                  () => {
                      debounce = true;
                  },
              500,
              );
              return true;
          }
          return false;
      },
    /**
     * For updating current route params. For example the nav bar title and
     * buttons are based on the route params.
     * This means `setParams` can be used to update nav bar for example.
     */
    setParams: (params: NavigationParams): boolean =>
      navigation.dispatch(
        NavigationActions.setParams({
          params,
          key: navigation.state.key,
        }),
      ),
  };
}

wuyunqiang avatar Nov 17 '17 08:11 wuyunqiang

第二种方式(建议使用):

// 主要是这一步
const navigateOnce = (getStateForAction) => (action, state) => {
  console.log('执行了这里跳转页面')
  const {type, routeName} = action
  return (
        state &&
        type === NavigationActions.NAVIGATE &&
        routeName === state.routes[state.routes.length - 1].routeName
    ) ? null : getStateForAction(action, state)
}

// 这是第二步
AppNavigator.router.getStateForAction = navigateOnce(AppNavigator.router.getStateForAction)

wuyunqiang avatar Jan 15 '18 09:01 wuyunqiang