reactjs - 有没有办法在反应JS中的一段时间内更改图像
问题描述
我想每 3 秒更改一次左侧的图像。是否有任何技术可以在不使用反应图像滑块的情况下做到这一点?
我在这里附上了我的代码。我曾尝试使用反应钩子来做到这一点。我已将图像存储在数组中。图像仅在前 8-10 秒内完美变化。
import React, { useEffect, useState } from "react";
import bannerImg1 from "../../Assets/Images/bannerProduct1.png";
import bannerImg2 from "../../Assets/Images/bannerProduct2.png";
import { Row, Col, Container } from "react-bootstrap";
import { FaCaretRight, FaChevronLeft, FaChevronRight } from "react-icons/fa";
import "./BannerSlider.css";
const BannerSlider = () => {
const imageArray = [bannerImg1, bannerImg2];
let [count, setCount] = useState(0);
const [image, setImage] = useState(imageArray[count]);
useEffect(() => {
setImage(imageArray[count]);
}, [count]);
useEffect(() => {
setInterval(() => {
setCount(count + 1);
}, 3000);
}, []);
return (
<div className="bannerSlider">
<Container>
<div className="slider-wrapper">
<Row className="d-flex align-items-center">
<Col lg={6} className="banner-image">
<img src={image} className="w-100 img-fluid" alt="" />
</Col>
<Col lg={6} className="banner-text">
<h2>Where your imagination come true</h2>
<p>
{" "}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo
porro neque odio, at aspernatur explicabo?{" "}
</p>
<button>
Explore More <FaCaretRight className="banner-button-icon" />{" "}
</button>
</Col>
</Row>
</div>
</Container>
</div>
);
};
解决方案
问题
根据我对您的代码的阅读,您有一些问题:
useEffect
卸载组件时不会清除任何正在运行的计时器。- 您已经关闭了
count
间隔回调中的初始状态,因此它没有正确更新。 - 您不考虑增加超过数组长度。
解决方案
// move image array outside component so it's not a dependency
const imageArray = [bannerImg1, bannerImg2, bannerImg3];
const BannerSlider = () => {
const [count, setCount] = useState(0);
// Save timer ref and return cleanup function to clear it
useEffect(() => {
const timerId = setInterval(() => {
// Use a functional state update to correctly increment the count
setCount(count => count + 1);
}, 3000);
return () => clearInterval(timerId);
}, []);
// `image` is derived state from the image array and current count
// Take the count mod array length to get the correct computed index
const image = imageArray[count % imageArray.length];
return (
<div className="bannerSlider">
<Container>
<div className="slider-wrapper">
<Row className="d-flex align-items-center">
<Col lg={6} className="banner-image">
<img src={image} className="w-100 img-fluid" alt="" />
</Col>
<Col lg={6} className="banner-text">
<h2>Where your imagination come true</h2>
<p>
{" "}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo
porro neque odio, at aspernatur explicabo?{" "}
</p>
<button>
Explore More <FaCaretRight className="banner-button-icon" />{" "}
</button>
</Col>
</Row>
</div>
</Container>
</div>
);
};
推荐阅读
- java - 无法引用 Listview id(帮助!)
- scala - Scala 在 setter 中自动创建函数
- python - 如何在 PyCharm 中调试外部 python 包
- ios - Flutter:Podfile 和 Apple Watch
- android - 带有 clicable 链接的 EditText,但在用户自己更改之前不可编辑/Kotlin,Android
- javascript - ReactJS:更新对象状态内的数组不会触发重新渲染
- php - Codeigniter 在辅助函数中的函数中使用函数
- python - 在 DatetimeIndex 上过滤 Pandas 系列 value_counts
- html - 如何在不改变卡片高度或干扰其他元素位置的情况下添加元素
- javascript - 当我拥有所有参数时,为什么轮播不起作用?