首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用React-Window键入传递给Row函数的索引和样式属性

如何使用React-Window键入传递给Row函数的索引和样式属性
EN

Stack Overflow用户
提问于 2020-12-08 18:24:41
回答 1查看 125关注 0票数 1

我尝试输入以下两个传递给Row函数的参数。

代码语言:javascript
复制
//https://github.com/bvaughn/react-window

import * as React from "react";
import styled from "styled-components";
import { FixedSizeList as List } from "react-window";
import AutoSizer from "react-virtualized-auto-sizer";

const StyledSection = styled.section`
  width: 100vw;
  height: 100vh;
  background: #C0C0C0;
`;

const Row = ({ index, style }) => (
  <div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
    Row {index}
  </div>
);

const Scroller = () => {
  return (
    <StyledSection>
      <AutoSizer>
        {({ height, width }) => {
          return (
            <List height={height} width={width} itemSize={20} itemCount={300}>
              {Row}
            </List>
          );
        }}
      </AutoSizer>
    </StyledSection>
  );
};

export { Scroller };

因此,下面的代码片段typescript将索引和样式参数推断为类型any。我试着将index的类型内联为number,但是编译器说index没有定义。

代码语言:javascript
复制
   const Row = ({ index, style }) => (
      <div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
        Row {index}
      </div>
    );

如何为这两个参数提供类型。react-window有自己的d.ts文件。这是工作代码working https://codesandbox.io/s/infinite-scroller-52b7d?file=/src/Scroller.tsx

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-08 19:05:37

这就是你想要的吗?

代码语言:javascript
复制
import { CSSProperties } from 'react';

const Row = ({ index, style }: { index: number; style: CSSProperties; }) => (
  <div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
    Row {index}
  </div>
);

在使用对象解构时,您仍然可以添加类型。您还可以使用集成开发环境查找style的类型,方法是在style={style}的第一个style上使用goto declaration。( jetbrain IDE中的ctrl-b或ctrl-q,对于VScode不知道,对不起)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65197118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档