这也是我第一次在这里发帖,我对Javascript和React非常陌生。这里,我有反应-无限-滚动-组件的代码。我想将这个类组件转换成一个功能组件:
类组件:
import React from "react";
import { render } from "react-dom";
import InfiniteScroll from "react-infinite-scroll-component";
const style = {
height: 30,
border: "1px solid green",
margin: 6,
padding: 8
};
class App extends React.Component {
state = {
items: Array.from({ length: 20 }),
hasMore: true
};
fetchMoreData = () => {
if (this.state.items.length >= 500) {
this.setState({ hasMore: false });
return;
}
// a fake async api call like which sends
// 20 more records in .5 secs
setTimeout(() => {
this.setState({
items: this.state.items.concat(Array.from({ length: 20 }))
});
}, 500);
};
render() {
return (
<div>
<h1>demo: react-infinite-scroll-component</h1>
<hr />
<InfiniteScroll
dataLength={this.state.items.length}
next={this.fetchMoreData}
hasMore={this.state.hasMore}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: "center" }}>
<b>Yay! You have seen it all</b>
</p>
}
>
>
{this.state.items.map((i, index) => (
<div style={style} key={index}>
div - #{index}
</div>
))}
</InfiniteScroll>
</div>
);
}
}
render(<App />, document.getElementById("root"));我尝试将其转换为功能组件,但失败了。请更正以下代码中的错误:
import { React, useState } from "react";
import { render } from "react-dom";
import InfiniteScroll from "react-infinite-scroll-component";
const style = {
height: 30,
border: "1px solid green",
margin: 6,
padding: 8
};
function App() {
const {items, hasMore} = useState(Array.from({ length: 20 }), true)
fetchMoreData = () => {
if (items.length >= 500) {
setState({ hasMore: false });
return;
}
// a fake async api call like which sends
// 20 more records in .5 secs
setTimeout(() => {
setState({
items: items.concat(Array.from({ length: 20 }))
});
}, 500);
};
return (
<div>
<h1>demo: react-infinite-scroll-component</h1>
<hr />
<InfiniteScroll
dataLength={items.length}
next={fetchMoreData}
hasMore={state.hasMore}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: "center" }}>
<b>Yay! You have seen it all</b>
</p>
}
>
>
{state.items.map((i, index) => (
<div style={style} key={index}>
div - #{index}
</div>
))}
</InfiniteScroll>
</div>
);
}
render(<App />, document.getElementById("root"));我知道我的代码充满了错误和非常脏,但请原谅我,并感谢提前!
发布于 2022-07-08 12:45:43
你可以试试这个
import { React, useState } from "react";
import { render } from "react-dom";
import InfiniteScroll from "react-infinite-scroll-component";
const style = {
height: 30,
border: "1px solid green",
margin: 6,
padding: 8
};
function App() {
const [items, setItems] = useState(Array.from({ length: 20 }))
const [hasMore, setHasMore] = useState(true)
fetchMoreData = () => {
if (items.length >= 500) {
setHasMore(false);
return;
}
// a fake async api call like which sends
// 20 more records in .5 secs
setTimeout(() => {
setItems(items.concat(Array.from({ length: 20 })));
}, 500);
};
return (
<div>
<h1>demo: react-infinite-scroll-component</h1>
<hr />
<InfiniteScroll
dataLength={items.length}
next={fetchMoreData}
hasMore={hasMore}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: "center" }}>
<b>Yay! You have seen it all</b>
</p>
}
>
>
{items.map((i, index) => (
<div style={style} key={index}>
div - #{index}
</div>
))}
</InfiniteScroll>
</div>
);
}
render(<App />, document.getElementById("root"));https://stackoverflow.com/questions/72911557
复制相似问题