gpt4 book ai didi

javascript - 如何将 Prop 和功能传递给 react-widow 列表?

转载 作者:行者123 更新时间:2023-11-30 19:20:46 25 4
gpt4 key购买 nike

这是 react-window 插件:https://github.com/bvaughn/react-window

我正在使用它来呈现“行”的简单列表。

这是 Row comp,我尝试在其中传递函数和 const idTestProps=''

class Row extends PureComponent {
render() {
const { index, style } = this.props;
let label;
if (itemStatusMap[index] === LOADED) {
label = `Row ${index}`;
} else {
label = "Loading...";
}
return (
<div className="ListItem" style={style}>
{label}
</div>
);
}
}

这是 Container 组件,它应该将函数和一个属性传递给 Row 组件:

const outerElementType = forwardRef((props, ref) => (
<div ref={ref} onClick={handleClick} {...props} />
));

export default function App() {
return (
<Fragment>
<InfiniteLoader
isItemLoaded={isItemLoaded}
itemCount={1000}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
className="List"
height={150}
itemCount={1000}
itemSize={35}
// This is outerElementType is way to pass some function down to Row
outerElementType={outerElementType}
width={300}
>
{Row}
</List>
)}
</Fragment>
);

我成功地传递了“函数”并且可以工作,但是 property 没有。

props如何与function同时传递?

这是 codesandbox 示例:<强> https://codesandbox.io/s/4zqx79nww0

最佳答案

我从未使用过 react-window 但也许你可以这样做:

import React, { forwardRef } from "react";
import ReactDOM from "react-dom";
import { FixedSizeList as List } from "react-window";

import "./styles.css";

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

function handleOnWheel({ deltaY }) {
// Your handler goes here ...
console.log("handleOnWheel()", deltaY);
}

const outerElementType = forwardRef((props, ref) => (
<div ref={ref} onWheel={handleOnWheel} {...props} />
));

const Example = () => (
<List
className="List"
height={150}
itemCount={1000}
itemSize={35}
outerElementType={outerElementType}
width={300}
>
{Row({ test: "test" })}
</List>
);

ReactDOM.render(<Example />, document.getElementById("root"));

关于javascript - 如何将 Prop 和功能传递给 react-widow 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com