简体   繁体   English

react-navigation 导航钩子类型

[英]react-navigation navigation hook type

I am using this hook from react-navigation and instead of calling the hook again in different components, I want to pass navigation as a prop.我正在使用 react-navigation 中的这个钩子,而不是在不同的组件中再次调用这个钩子,我想将navigation作为道具传递。

const navigation = useNavigation();
...
  <MyButton
     resetLocation={resetLocation}
     navigation={navigation}
  /> 

But I am unable to figure out the "type" for this navigation prop that I should pass into my MyButton component.但是我无法弄清楚我应该传递给 MyButton 组件的这个导航道具的“类型”。

type MyButtonProps = {
  resetLocation: (locationToReset: string) => void;
  navigation: any;
};

export const MyButton: React.FunctionComponent<MyButtonProps> = ({
  resetLocation,
  navigation,

What should I replace navigation: any;我应该用什么代替navigation: any; with?和?

Use NavigationProp<ParamListBase>使用NavigationProp<ParamListBase>

Here is the definition of useNavigation, I pasted it here just in case.这里是 useNavigation 的定义,我把它贴在这里以防万一。

import * as React from 'react';
import type { ParamListBase } from '@react-navigation/routers';
import NavigationContext from './NavigationContext';
import type { NavigationProp } from './types';

/**
 * Hook to access the navigation prop of the parent screen anywhere.
 *
 * @returns Navigation prop of the parent screen.
 */
export default function useNavigation<
  T extends NavigationProp<ParamListBase>
>(): T {
  const navigation = React.useContext(NavigationContext);

  if (navigation === undefined) {
    throw new Error(
      "Couldn't find a navigation object. Is your component inside a screen in a navigator?"
    );
  }

  return navigation as T;
}

Here is the link for more info https://github.com/react-navigation/react-navigation/blob/main/packages/core/src/useNavigation.tsx这是更多信息的链接https://github.com/react-navigation/react-navigation/blob/main/packages/core/src/useNavigation.tsx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM