【问题标题】:react navigation: how to pass redux props with NavigationStackScreenComponent type in typescript反应导航:如何在打字稿中使用 NavigationStackScreenComponent 类型传递 redux 道具
【发布时间】:2020-05-13 21:48:41
【问题描述】:

我开始在我的 react native 项目(typescript 新手)中使用 typescript,并在使用 typescript 的功能组件中遇到了 navigationOptions 问题,感谢这篇文章 here 我解决了这个问题,但是当我尝试传递 redux 道具时它告诉我

Property 'authType' does not exist on type 'PropsWithChildren<NavigationStackScreenProps<Params, Props>>

这是我的代码

interface Props {
 authType: string;
}

interface Params {
 routeName: string;
}

const Customer: NavigationStackScreenComponent<Params, Props> = ({
 navigation,
 authType,
 }) => {
 .... Component
 }

 Customer.navigationOptions = ({ navigation }) => ({
   title: navigation.getParam('routeName'),
 });

我认为这意味着我正在传递的道具没有被读取,尽管我正在传递它。

我使用 NavigationStackScreenComponent 是为了能够使用

Customer.navigationOptions etc

否则它会告诉我

ts] Property 'navigationOptions' does not exist

我真的开始爱上打字稿了,知道如何使用 authType Props 而不会让我尖叫吗?

【问题讨论】:

    标签: typescript react-native react-navigation react-navigation-stack


    【解决方案1】:

    我最终这样做了

    type Params = {
      routeName: string;
    };
    
    type Props = {
      authType?: string;
      navigation: NavigationStackProp;
    };
    
    const Customer: NavigationStackScreenComponent<Params> = ({
      authType,
      navigation,
    }: Props) => {
    

    我只是将传递道具设为可选。

    【讨论】:

      【解决方案2】:

      你需要做类似的事情

      interface Props {
       authType: string;
      }
      
      type CustomerType = React.ComponentType<Props & NavigationStackScreenProps> & { navigationOptions: NavigationStackOptions | ((props: NavigationStackScreenProps) => NavigationStackOptions) }
      
      const Customer: CustomerType = ({
       navigation,
       authType,
      }) => {
       .... Component
      }
      

      【讨论】:

      • 它有效,但我们又回到了原点告诉我Property 'navigationOptions' does not exist on type 'FunctionComponent&lt;Props &amp; NavigationStackScreenProps&lt;Params, unknown&gt;&gt;'
      猜你喜欢
      • 2021-09-12
      • 2021-06-20
      • 1970-01-01
      • 2020-08-21
      • 2018-06-25
      • 2018-10-26
      • 1970-01-01
      • 2022-11-28
      • 2021-05-20
      相关资源
      最近更新 更多