[英]Show List of Images in React with Pagination
I am very new to React so please help me out here.我对 React 很陌生,所以请在这里帮助我。 I have a simple React Function Component that has an image tag and I would like to use this image component in my main component and render it.
我有一个简单的 React Function 组件,它有一个图像标签,我想在我的主要组件中使用这个图像组件并渲染它。 That works fine.
这很好用。
I want to render the same Image component 10 - 50 times with Pagination.我想用分页渲染相同的图像组件 10-50 次。 Can someone send me a link or help me how can I show the list of images with the function component containing Pagination.
有人可以给我发送链接或帮助我如何使用包含分页的 function 组件显示图像列表。
Image Component图像组件
function ImageFunctionComponent(){
return (
<img alt='Test'
src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcStvpLEylZEJ-dJLEYU3ApbyDooj-_98dYQuw&usqp=CAU"/>
);
}
export default ImageFunctionComponent;
index.js index.js
class ImageTest extends React.Component {
render(){
return(
<>
<ImageFunctionComponent></ImageFunctionComponent>
<ImageFunctionComponent></ImageFunctionComponent>
<ImageFunctionComponent></ImageFunctionComponent>
<ImageFunctionComponent></ImageFunctionComponent>
<ImageFunctionComponent></ImageFunctionComponent>
<ImageFunctionComponent></ImageFunctionComponent>
</>
)
;
}
}
const element=<ImageTest />
ReactDOM.render(element,document.getElementById("root"));
I want to only show two images on one page and then have a pagination to show next two images.我只想在一页上显示两个图像,然后有一个分页来显示接下来的两个图像。 How can I do that in React?
我如何在 React 中做到这一点?
firstly for this specific case.首先针对这个具体案例。 I would store the images in an array inside the pagination component and also store the page counter like this
我会将图像存储在分页组件内的数组中,并像这样存储页面计数器
const LIMIT = 2;
const [images, setImages] = useState([<ImageFunctionComponent/>, <ImageFunctionComponent/>, ...]);
const [page, setPageCounter] = useState(0);
then bind the left arrow of the pagination to: setPageCounter(page - 1); // taking in count if page is not zero
然后将分页的左箭头绑定到:
setPageCounter(page - 1); // taking in count if page is not zero
setPageCounter(page - 1); // taking in count if page is not zero
and the right arrow to setPageCounter(page + 1); // taking in count page * LIMIT < array.length
setPageCounter(page - 1); // taking in count if page is not zero
,向右箭头setPageCounter(page + 1); // taking in count page * LIMIT < array.length
setPageCounter(page + 1); // taking in count page * LIMIT < array.length
then in the render is just to return:然后在渲染中只是返回:
const renderImages = [];
for(let i = page*LIMIT; i < page*LIMIT + LIMIT; i++) {
renderImages.push(images[i]);
}
return (<>
renderImages
</>);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.