简体   繁体   English

如何通过 typescript 描述 React-Native(-Web) 中的 ScrollEvent 类型?

[英]How to describe the ScrollEvent type in React-Native(-Web) via typescript?

I can't figure out how to describe the ScrollEvent type in React-Native-Web but I suppose the same is actual for the React Native?我不知道如何在 React-Native-Web 中描述 ScrollEvent 类型,但我想 React Native 也是如此?

To be honest, it's always a bit hard for me to correctly describe the Event types for the React native (and the standard React as well) because I haven't found any common documentation, etc. about it so I always used老实说,我总是很难正确描述 React 原生(以及标准 React)的事件类型,因为我没有找到任何关于它的通用文档等,所以我一直使用

In general, we can use the Generic Synthetic Event and we can pass the definition of the required type into this generic.一般来说,我们可以使用通用合成事件,我们可以将所需类型的定义传递给这个泛型。 I guess in my case it must be something like ScrollEvent but I don't have any idea what exactly should be located there?我想在我的情况下它必须是类似ScrollEvent的东西,但我不知道究竟应该在那里放置什么?

I tried to use the standard React.SynteticEvent type however I received an error我尝试使用标准的React.SynteticEvent类型但是我收到了一个错误

Property 'contentOffset' doesn't exist on type Event事件类型上不存在属性“contentOffset”

The code example代码示例

      onScroll={(e: React.SyntheticEvent) => {
        let y = 0;

        // the scroll data receives differently on mobile and web
        if (Platform.OS !== 'web') {
          // Property 'contentOffset' doesn't exist on type Event
          y = e.nativeEvent.contentOffset.y;
        } else {
          // Property 'target' doesn't exist on type Event
          y = e.nativeEvent.target.scrollTop;
        }

        if (inputLayout.scrollY !== y) {
          setInputLayout({
            ...inputLayout,
            scrollY: y,
          });
        }

        if (isFunction(onScroll)) {
          onScroll(e);
        }
      }}

Many thanks for any help/info/link to the documentation!非常感谢您提供任何帮助/信息/文档链接!

The right interface for Scroll Event is UIEvent . Scroll Event的正确接口是UIEvent

import React, { UIEvent } from 'react';

onScroll={(e: UIEvent<HTMLDivElement>) => {

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

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