You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Pendo的React Native JavaScript集成代码转换为TypeScript?请求针对函数式组件(FC)结构及Props对象的处理指引

Converting Pendo React Native Integration to TypeScript (FC Structure)

Let's break this down step by step, with a sharp focus on proper prop typing since that's your primary concern:

1. Define the Props Type

First, we need to explicitly type the props injected by withPendoRN into your component. The critical prop here is onStateChange, which accepts a NavigationState object from React Navigation. We'll start by importing the necessary type definitions:

import { NavigationState, NavigationContainerRef } from '@react-navigation/native';
import { FC, useRef } from 'react';
import { withPendoRN } from 'rn-pendo-sdk';

Next, create an interface for your component's props to enforce type safety:

interface RootNavigatorProps {
  // Prop injected by withPendoRN to handle navigation state changes
  onStateChange: (state: NavigationState) => void;
}

2. Refactor the Component to Typed FC

Rewrite your RootNavigator as a typed function component, and properly type the navigationRef to match React Navigation's container ref type—this eliminates vague any types and prevents runtime errors:

const RootNavigator: FC<RootNavigatorProps> = ({ onStateChange }) => {
  // Explicitly type the ref to match NavigationContainer's reference type
  const navigationRef = useRef<NavigationContainerRef>(null);

  return (
    <NavigationContainer 
      ref={navigationRef}
      onStateChange={() => {
        // Use optional chaining to safely access the ref (TypeScript enforces this for null safety)
        const state = navigationRef.current?.getRootState();
        if (state) {
          onStateChange(state);
        }
      }}
      onReady={() => {
        const state = navigationRef.current?.getRootState();
        if (state) {
          onStateChange(state);
        }
      }}
    >
      {MainStackScreen()}
    </NavigationContainer>
  );
};

3. Export with Pendo HOC (Type-Safe)

Finally, wrap and export the component with withPendoRN. TypeScript will automatically infer the prop types from our interface, but you can also be explicit if you prefer:

export default withPendoRN(RootNavigator);

Key Observations:

  • Null Safety: Added optional chaining (?.) when accessing navigationRef.current to handle cases where the ref might not be initialized yet—this is required in TypeScript to avoid null/undefined errors.
  • Type Dependencies: Ensure you have @types/react and @react-navigation/native installed (they should be included if you're using TypeScript with React Native and React Navigation).
  • FC Typing: Using FC<RootNavigatorProps> ensures TypeScript validates all props passed to the component, including the one injected by Pendo's higher-order component.

内容的提问来源于stack exchange,提问作者SJaka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 16:39:06