Fak*_* 10 6 typescript react-window
我正在尝试输入传递给 Row 函数的以下两个参数。
//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 };
Run Code Online (Sandbox Code Playgroud)
因此,以下代码片段将索引和样式参数推断为任何类型。我尝试将索引的类型内联为数字,但编译器说索引未定义。
const Row = ({ index, style }) => (
<div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
Row {index}
</div>
);
Run Code Online (Sandbox Code Playgroud)
任何想法如何为这两个参数提供类型。React-Window 有自己的 d.ts 文件。这是工作的codesandbox - https://codesandbox.io/s/infinite-scroller-52b7d?file=/src/Scroller.tsx
这就是你所寻求的吗?
import { CSSProperties } from 'react';
const Row = ({ index, style }: { index: number; style: CSSProperties; }) => (
<div className={index % 2 ? 'ListItemOdd' : 'ListItemEven'} style={style}>
Row {index}
</div>
);
Run Code Online (Sandbox Code Playgroud)
使用对象解构时,您仍然可以添加类型。您还可以使用 IDE 来查找 的类型,在 的第一个上style使用。(jetbrain IDE 中的 ctrl-b 或 ctrl-q,不知道 VScode,抱歉)。goto declarationstylestyle={style}
| 归档时间: |
|
| 查看次数: |
2814 次 |
| 最近记录: |