我有一个被Axios成功提取的URL
const URL_INTERIORES = 'http://localhost:3001/interiores';我从npm安装了react-image-lightbox,它为我提供了在数组中配置的默认镜像。
const images = [
'//placekitten.com/1500/500',
'//placekitten.com/4000/3000',
'//placekitten.com/800/1200',
'//placekitten.com/1500/1500',
];我想更改默认数组,以便将db.json文件中的图像放入图像的lightbox中。我该怎么解决它呢?下面是代码的其余部分,带有'react-image-lightbox‘配置:
class Interiores extends Component {
constructor(props) {
super(props)
this.state = {
interiores: [],
photoIndex: 0,
isOpen: false
}
}
componentDidMount() {
axios.get(URL_INTERIORES)
.then(res => {
this.setState({ interiores: res.data })
})
}
render() {
const { photoIndex, isOpen } = this.state;
return (
<div>
<button type="button" onClick={() => this.setState({ isOpen: true })}>
Open Lightbox
</button>
{isOpen && (
<Lightbox
mainSrc={images[photoIndex]}
nextSrc={images[(photoIndex + 1) % images.length]}
prevSrc={images[(photoIndex + images.length - 1) % images.length]}
onCloseRequest={() => this.setState({ isOpen: false })}
onMovePrevRequest={() =>
this.setState({
photoIndex: (photoIndex + images.length - 1) % images.length,
})
}
onMoveNextRequest={() =>
this.setState({
photoIndex: (photoIndex + 1) % images.length,
})
}
/>
)}
</div>
)
}
}
export default Interiores;这是我的db.json文件。
"interiores": [
{
"text": "introduction text here",
"images": [
"int_01_thumb.jpg", "int_02_thumb.jpg", "int_03_thumb.jpg",
"int_04_thumb.jpg", "int_05_thumb.jpg", "int_06_thumb.jpg",
"int_07_thumb.jpg", "int_08_thumb.jpg", "int_09_thumb.jpg"
]
}
],https://stackoverflow.com/questions/51371928
复制相似问题