关于antd-mobile的使用问题

发布于 2022-09-12 02:27:01 字数 3906 浏览 43 评论 0

我想轮播三组图片,每组四张,点击下一步之后从第一张开始,
但是问题来了,头一组图片播放到第三张时,我点击下一步,他会自动播放第二组图片的第四张,不是从第一张开始的,这个怎么解决?
附上代码

import React, { useState, useEffect } from 'react';
import { setTitle } from '../../utils/util';
import { Carousel } from 'antd-mobile';
import { launch, ourSign, otherSign } from '../../utils/constants';
import finish from '../../images/noviceGuide/finish.png';

import './index.less';

const NoviceGuide = ({ history }) => {

  const [ imgs, setImgs ] = useState(launch);

  let [ step, setStep ] = useState(1);

  const [ interval, setAutoplayInterval ] = useState(100);

  useEffect(() => {
    setTitle('新手引导');
    setAutoplayInterval(3000);
  },[])

  const showImgs = (step) => {
    if (step === 1) {
      setImgs(launch)
    } else if (step === 2) {
      setImgs(ourSign)
    } else {
      setImgs(otherSign)
    }
  }

  // 下一步
  const next = () => {
    setStep(++step)
    showImgs(step);
  }

  // 上一步
  const prev = () => {
    setStep(--step)
    showImgs(step);
  }

  // 返回首页
  const goHome = () => {
    history.replace('/home')
  }

  // 步骤条
  const renderSteps = () => {
    if (step === 1) {
      return  <div className="novice-guide-steps">
        <p className="novice-guide-steps-first">1</p>
        <p className="novice-guide-steps-text">发起签署</p>
        <p className="novice-guide-steps-second">2</p>
        <p className="novice-guide-steps-third">3</p>
      </div>
    } else if (step === 2) {
      return  <div className="novice-guide-steps">
        <p className="novice-guide-steps-first-finish"><img src={finish} alt="finish" /></p>
        <p className="novice-guide-steps-second-now">2</p>
        <p className="novice-guide-steps-second-text">对方签署</p>
        <p className="novice-guide-steps-third">3</p>
      </div>
    } else {
      return  <div className="novice-guide-steps">
        <p className="novice-guide-steps-first-end"><img src={finish} alt="finish" /></p>
        <p className="novice-guide-steps-second-finish"><img src={finish} alt="finish" /></p>
        <p className="novice-guide-steps-third-now">3</p>
        <p className="novice-guide-steps-third-text">我方签署</p>
      </div>
    }
  }

  // 底部按钮
  const renderBtn = () => {
    if (step === 1) {
      return <div className="novice-guide-btn">
        <p className="novice-guide-btn-next-first" onClick={next}>下一步</p>
      </div>
    } else if (step === 2) {
      return <div className="novice-guide-btn">
        <p className="novice-guide-btn-prev" onClick={prev}>上一步</p>
        <p className="novice-guide-btn-next" onClick={next}>下一步</p>
      </div>
    } else {
      return <div className="novice-guide-btn">
        <p className="novice-guide-btn-prev" onClick={prev}>上一步</p>
        <p className="novice-guide-btn-next" onClick={goHome}>我学会了</p>
      </div>
    }
  }
  
  return (
    <div className="novice-guide-wrap">
      <div className="novice-guide">
        {renderSteps()}
        <div className="novice-guide-carousel">
          <Carousel autoplay infinite dotActiveStyle={{ backgroundColor: '#4EA3ED' }} autoplayInterval={interval}>
            {imgs.map((item, index) => (
              <img key={index} src={item} alt=''/>
            ))}
          </Carousel>
        </div>
        {step === 3 && <p className="novice-guide-point">可以从帮助中心再次查看</p>}
      </div>
      {renderBtn()}
    </div>
  )
}

export default NoviceGuide;

launch,ourSign,otherSign就是每组图片,格式为[img1, img2, img3]
我希望当我点击下一步时,轮播图能变成第二组图片的第一张,而不是按照slide显示

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2022-09-19 02:27:01

不这么写了,我感觉很有可能是共享state的问题,我直接换了暴力的写法,直接写了三个carousel!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文