来自 api 的反应图像标题错误可能与正确的索引有关
React images caption error from api maybe to do with correct Index
后续来自:
我的应用使用 react-images 作为灯箱,使用 react-carousel-images 作为轮播。我的 api 有标题和图片。我遇到的问题是页脚标题中的标题总是随着所选图像的出现而出现,并且在灯箱轮播中不会改变。我认为这与所选图像的索引有关?到目前为止我的代码是:
问题示例:
https://codesandbox.io/s/react-multi-corousal-issue-72s9o?file=/src/Slider.js
slider.js
import React, { Component } from "react";
// import "../slider/slider.css";
import "./slider.css";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
import LightBox, { Modal, ModalGateway } from "react-images";
const responsive = {
superLargeDesktop: {
breakpoint: { max: 4000, min: 3000 },
items: 1
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 1
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1
}
};
class Slider extends Component {
_isMounted = false;
state = {
awsApiData: [],
loading: false,
//selectedIndex: 0,
selectedImage: {},
lightboxIsOpen: false
};
componentDidMount() {
this._isMounted = true;
console.log("app mounted");
this.setState({ loading: true });
/*global fetch */
fetch("https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/livestage/imgApi")
.then(data => data.json())
.then(data =>
// this.setState({ awsApiData: data[0], loading: false }, () =>
this.setState(
{
// awsApiData: data.map(item => ({source: item.download_url })),
awsApiData: data.map(item => ({
...item,
source: item.image
})),
loading: false
},
() => console.log(data)
)
);
}
componentWillUnmount() {
this._isMounted = false;
}
toggleLightbox = (post, selectedIndex) => {
// this.setState(state => ({
// lightboxIsOpen: !state.lightboxIsOpen,
// selectedIndex
// }));
this.setState(state => ({
lightboxIsOpen: !state.lightboxIsOpen,
selectedImage: { title: post.title, index: selectedIndex }
}));
};
render() {
return (
<div>
{this.state.loading ? (
<div className="text-center">Loading...</div>
) : (
<div>
<Carousel
additionalTransfrom={0}
showDots={false}
arrows={true}
autoPlaySpeed={3000}
autoPlay={true}
centerMode={false}
className="slider"
containerClass="container-with-dots"
dotListClass="dots"
draggable
focusOnSelect={false}
infinite
itemClass="carousel-top"
keyBoardControl
minimumTouchDrag={80}
renderButtonGroupOutside={false}
renderDotsOutside
responsive={responsive}
>
{Object.values(this.state.awsApiData).map((post, indx) => {
return (
<div
className="mt-5"
key={indx}
//onClick={() => this.toggleLightbox(indx)}
onClick={() => this.toggleLightbox(post, indx)}
>
<img
className="media-img card-img-top card-img-hero"
src={post.source}
alt="Alt text"
style={{ cursor: "pointer" }}
/>
</div>
);
})}
</Carousel>
<ModalGateway>
{this.state.lightboxIsOpen ? (
<Modal onClose={this.toggleLightbox}>
<LightBox
components={{
FooterCaption: () => <div>{this.state.selectedImage.title}</div>
}}
//currentIndex={this.state.selectedIndex}
currentIndex={this.state.selectedImage.index}
// formatters={{ getAltText }}
frameProps={{ autoSize: "height" }}
views={this.state.awsApiData}
/>
</Modal>
) : null}
</ModalGateway>
</div>
)}
</div>
);
}
}
export default Slider;
您的标题设置为 this.state.selectedImage.title
,但您从未在灯箱视图更改时更新状态(即调用 setState()
)。
您需要将 onViewChange
处理程序添加到 LightBox
组件:
<LightBox onViewChange={handleLightBoxViewChange} .../>
该处理程序也将接收视图更改的项目的索引。在 handleLightBoxViewChange
调用 setState
更新 selectedImage
:
handleLightBoxViewChange = (newIndex) => {
this.setState(state => ({
selectedImage: state.awsApiData[newIndex]
}));
};
如果您不想更新状态,您的页脚组件会收到当前视图,因此您可以像这样更新它:
<LightBox
components={{
FooterCaption: ({currentView}) => <div>{currentView.title}</div>
}}
//...
/>
后续来自:
我的应用使用 react-images 作为灯箱,使用 react-carousel-images 作为轮播。我的 api 有标题和图片。我遇到的问题是页脚标题中的标题总是随着所选图像的出现而出现,并且在灯箱轮播中不会改变。我认为这与所选图像的索引有关?到目前为止我的代码是:
问题示例: https://codesandbox.io/s/react-multi-corousal-issue-72s9o?file=/src/Slider.js
slider.js
import React, { Component } from "react";
// import "../slider/slider.css";
import "./slider.css";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
import LightBox, { Modal, ModalGateway } from "react-images";
const responsive = {
superLargeDesktop: {
breakpoint: { max: 4000, min: 3000 },
items: 1
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 1
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1
}
};
class Slider extends Component {
_isMounted = false;
state = {
awsApiData: [],
loading: false,
//selectedIndex: 0,
selectedImage: {},
lightboxIsOpen: false
};
componentDidMount() {
this._isMounted = true;
console.log("app mounted");
this.setState({ loading: true });
/*global fetch */
fetch("https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/livestage/imgApi")
.then(data => data.json())
.then(data =>
// this.setState({ awsApiData: data[0], loading: false }, () =>
this.setState(
{
// awsApiData: data.map(item => ({source: item.download_url })),
awsApiData: data.map(item => ({
...item,
source: item.image
})),
loading: false
},
() => console.log(data)
)
);
}
componentWillUnmount() {
this._isMounted = false;
}
toggleLightbox = (post, selectedIndex) => {
// this.setState(state => ({
// lightboxIsOpen: !state.lightboxIsOpen,
// selectedIndex
// }));
this.setState(state => ({
lightboxIsOpen: !state.lightboxIsOpen,
selectedImage: { title: post.title, index: selectedIndex }
}));
};
render() {
return (
<div>
{this.state.loading ? (
<div className="text-center">Loading...</div>
) : (
<div>
<Carousel
additionalTransfrom={0}
showDots={false}
arrows={true}
autoPlaySpeed={3000}
autoPlay={true}
centerMode={false}
className="slider"
containerClass="container-with-dots"
dotListClass="dots"
draggable
focusOnSelect={false}
infinite
itemClass="carousel-top"
keyBoardControl
minimumTouchDrag={80}
renderButtonGroupOutside={false}
renderDotsOutside
responsive={responsive}
>
{Object.values(this.state.awsApiData).map((post, indx) => {
return (
<div
className="mt-5"
key={indx}
//onClick={() => this.toggleLightbox(indx)}
onClick={() => this.toggleLightbox(post, indx)}
>
<img
className="media-img card-img-top card-img-hero"
src={post.source}
alt="Alt text"
style={{ cursor: "pointer" }}
/>
</div>
);
})}
</Carousel>
<ModalGateway>
{this.state.lightboxIsOpen ? (
<Modal onClose={this.toggleLightbox}>
<LightBox
components={{
FooterCaption: () => <div>{this.state.selectedImage.title}</div>
}}
//currentIndex={this.state.selectedIndex}
currentIndex={this.state.selectedImage.index}
// formatters={{ getAltText }}
frameProps={{ autoSize: "height" }}
views={this.state.awsApiData}
/>
</Modal>
) : null}
</ModalGateway>
</div>
)}
</div>
);
}
}
export default Slider;
您的标题设置为 this.state.selectedImage.title
,但您从未在灯箱视图更改时更新状态(即调用 setState()
)。
您需要将 onViewChange
处理程序添加到 LightBox
组件:
<LightBox onViewChange={handleLightBoxViewChange} .../>
该处理程序也将接收视图更改的项目的索引。在 handleLightBoxViewChange
调用 setState
更新 selectedImage
:
handleLightBoxViewChange = (newIndex) => {
this.setState(state => ({
selectedImage: state.awsApiData[newIndex]
}));
};
如果您不想更新状态,您的页脚组件会收到当前视图,因此您可以像这样更新它:
<LightBox
components={{
FooterCaption: ({currentView}) => <div>{currentView.title}</div>
}}
//...
/>