【问题标题】:Typescript forwardRef error in react native反应本机中的Typescript forwardRef错误
【发布时间】:2020-12-27 18:13:47
【问题描述】:

我有入职和幻灯片组件。我将 ref 作为道具从带有 forwardRef 的入职组件中滑动组件。它工作正常,但打字稿给我一个错误

我的组件如下所示

const Onboarding = () => {
  const { width } = useOrientation();
  const scrollX = new Animated.Value(0);
  const scroll = useRef<Animated.ScrollView>(null);
  const onScroll = (event: any) => {
    Animated.event([{ nativeEvent: { contentOffset: { x: scrollX } } }]);
  };

  return (
    <Box flex={1}>
      <Animated.ScrollView
        ref={scroll}
        scrollEventThrottle={16}
        onScroll={onScroll}
        horizontal
      >
        {slides.map((data, index) => (
          <Slide key={index} data={data} ref={scroll} {...{ index, scrollX }} />
        ))}
      </Animated.ScrollView>
    </Box>
  );
};

interface SlideProps {
  data: {
    label: string;
    description: string;
    src: string;
  };
  scrollX: Animated.Value<0>;
  index: number;
}

export const Slide = forwardRef<Animated.ScrollView, SlideProps>(
  ({ data, scrollX, index }: SlideProps, ref) => {
    const { width, height } = Dimensions.get("window");

    const aspect = height / width;

    return (
      <Box flex={1} width={width} backgroundColor="mainBackground">
        <TouchableOpacity
          onPress={() =>
              //error here
            ref?.current?.getNode().scrollTo({ x: width * (index + 1), y: 0 })
          }
        >
          <Text>heyy</Text>
        </TouchableOpacity>
      </Box>
    );
  }
);

错误是这样的; 类型 '((instance: ScrollView | null) => void) | 上不存在属性 'current' MutableRefObject'。 我该如何解决这个问题?谢谢。

【问题讨论】:

    标签: typescript react-native react-forwardref


    【解决方案1】:

    说实话,在这种情况下看到forwardRef 感觉很奇怪。我的意思是,您真正想要的是让 ref 引用 Animated.ScrollView,而不是 Slide 对象。

    选项 1(在我看来很难看):将 ref 作为 Slide 的属性传递(而不是作为 ref 本身)。

    选项 2(我更喜欢):让 Slide 告诉 Onboarding 新闻事件发生了,然后让 Onboarding 执行 scrollTo

    const Onboarding = () => {
      const { width } = useOrientation();
      const scrollX = new Animated.Value(0);
      const scroll = useRef<Animated.ScrollView>(null);
      const onScroll = (event: any) => {
        Animated.event([{ nativeEvent: { contentOffset: { x: scrollX } } }]);
      };
      const onPress = (width: number, index: number) => {
        scroll.current?.getNode().scrollTo({ x: width * (index + 1), y: 0 })
      }
    
      return (
        <Box flex={1}>
          <Animated.ScrollView
            ref={scroll}
            scrollEventThrottle={16}
            onScroll={onScroll}
            horizontal
          >
            {slides.map((data, index) => (
              <Slide key={index} onPress={onPress} data={data} {...{ index, scrollX }} />
            ))}
          </Animated.ScrollView>
        </Box>
      );
    };
    
    interface SlideProps {
      // same as before
      onPress: (width:number, index:number) => void
    }
    
    export const Slide = ({ data, scrollX, index, onPress }: SlideProps) => {
        const { width, height } = Dimensions.get("window");
    
        const aspect = height / width;
    
        return (
          <Box flex={1} width={width} backgroundColor="mainBackground">
            <TouchableOpacity
              onPress={() => onPress(width, index)}
            >
              <Text>heyy</Text>
            </TouchableOpacity>
          </Box>
        );
      }
    );
    

    【讨论】:

    • 感谢您的回答。为什么 forwardRef 很奇怪?你可以解释吗 ?我想理解它
    • 是的,当然! ForwardRef,如下所示:reactjs.org/docs/forwarding-refs.html,当您想要将由父级创建的 ref 转发给单个子级时。您要做的是在两个孩子之间共享参考。如果您在示例中看到,则在 Onboard 组件中创建滚动引用并将其转发到 Animated.ScrollView。
    • 感谢您的帮助,这对我理解有很大帮助
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2021-07-11
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多