简体   繁体   English

从反应应用程序的模态内部单击下一个和上一个按钮时,无法使动画(淡入淡出)在模态上工作

[英]unable to make the animation(fade) work on a modal when clicking the next and previous button from inside the modal in a react app

I am using a ReactJS application which is mapping over an array of objects displayed in a div.我正在使用 ReactJS 应用程序,它映射到 div 中显示的对象数组。 When I click each div then a modal appears which shows the corresponding objects data on modal.当我单击每个 div 时,会出现一个模态,其中显示模态上的相应对象数据。

It has a previous and next button on the modal.它在模式上有一个上一个和下一个按钮。 When you click on the next button then the modal displays the second objects data and further clicking it displays the third objects data.当您单击下一个按钮时,模式会显示第二个对象数据,进一步单击它会显示第三个对象数据。

Same goes for the previous button but in a reverse direction.前一个按钮也是如此,但方向相反。

I am using a react-reveal library for showing Fade animation on texts inside a modal.我正在使用 react-reveal 库在模式内的文本上显示 Fade animation。 When I click individual DIV then the modal appears with corresponding object data and animation works on that.当我单击单个 DIV 时,模态将显示相应的 object 数据,并且 animation 可以处理该数据。

But when I click Previous and Next button from INSIDE the modal then corresponding data of next object do appear but the animation doesnot work on the texts.但是当我从模式内部单击上一个和下一个按钮时,会出现下一个 object 的相应数据,但 animation 对文本不起作用。

How can I make the animation work upon clicking Prev and Next button from inside the MODAL.在 MODAL 中单击 Prev 和 Next 按钮后,如何使 animation 工作。

The working code is::工作代码是::

App.js应用程序.js

import React, {useState} from 'react';
import Fade from 'react-reveal/Fade';
import 'react-responsive-modal/styles.css';
import { Modal } from 'react-responsive-modal';
import "./App.css";

const App = () => {
  const [open, setOpen] = useState(false);
  const [index, setIndex] = useState(0);
  const [profile, setProfile] = useState({});

  const [profiles, setProfiles] = useState([
    {title: "First title", description: "It is first description"},
    {title: "Second title", description: "It is second description"},
    {title: "third title", description: "It is third description"},
  ]);

  const onOpenModal = (item) => {
    setOpen(true);
    setProfile(item);
  }
  const onCloseModal = () => setOpen(false);
  const handlePrev = () => {
    if(index > 0){
      let i = index - 1;
      setProfile(profiles[i]);
      setIndex(i);
    }
  }

  const handleNext = () => {
    if(index < profiles.length - 1){
      let i = index + 1;
      setProfile(profiles[i]);
      setIndex(i);
    }
  }

  return (
    <div>
      <h1>Application..............</h1>
      <div className="container">
        {
          profiles.map((p, i) => (
            <div key={i} className="item" onClick={() => onOpenModal(p)}>
              <h1>{p.title}</h1>
            </div>
          ))
        }
      </div>

      <Modal open={open} onClose={onCloseModal} center
        classNames={{
          overlay: 'customOverlay',
          modal: 'customModal',
        }}>
          <h2>Simple centered modal</h2>
          <Fade bottom delay={300}>
            <h5>{profile.title}</h5>
          </Fade>

          <Fade bottom delay={800}>
            <p>{profile.description}</p>
          </Fade>

          <div className="btn-group">
            <button className="btn" onClick={handlePrev}>Prev</button>
            <button className="btn" onClick={handleNext}>Next</button>
          </div>
          
      </Modal>

    </div>
  )
}
export default App;

App.css应用程序.css

.container{
  display: flex;
  justify-content: space-around;
  margin-top: 8rem;
}

.item{
  border: 2px solid;
  padding: 4rem;
}


.customOverlay{
  background: rgba(36, 123, 160, 0.7);
}
.customModal{
  background: #b2dbbf;
  max-width: 700px;
  width: 100%;
  font-size: 1.8rem;
}

.btn{
  padding: 0.5rem 1.3rem;
  margin-right: 20px;
  margin-left: 8px;
  cursor: pointer;
}

.btn-group{
  width: 34%;
  display: flex;
  justify-content: flex-end;
  margin-left: auto;
}

The sandbox working version is here:沙盒工作版本在这里:

https://codesandbox.io/s/long-sun-9u00h https://codesandbox.io/s/long-sun-9u00h

you have to force the animation by adding key to Fade elements.您必须通过将key添加到Fade元素来强制 animation。 see the codebox 见密码箱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM