Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
React Transition Group - i dont understand it well

I'm trying to make an array of images that changes image every few seconds.

I made it change randomly and now I'm trying to add transition so it so it will look prettier.

The only way I saw it's being possible with React is using react-transition-group.

so I have made this useEffect:

const [imageSrc, setImageSrc] = useState(image1);
  const inProp = useRef(true);

  const defaultStyle = {
    transition: `opacity 2s ease-in-out`,
    opacity: 0,
  };

  const transitionStyles = {
    entering: { opacity: 1 },
    entered: { opacity: 1 },
    exiting: { opacity: 0 },
    exited: { opacity: 0 },
  };

  useEffect(() => {
    const changeImage = () => {
      const imagesArr = [image1, image2, image3];
      const imagesLength = imagesArr.length;
      let random = Math.floor(Math.random() * imagesLength);
      setImageSrc(imagesArr[random]);
      inProp.current = !inProp.current;
    };

    setInterval(changeImage, 6000);
    return () => {
      clearInterval(changeImage);
    };
  }, []);
     <Transition in={inProp.current} timeout={300}>
                {(state) => (
                  <img
                    src={imageSrc}
                    alt="about-img"
                    className="about-img"
                    style={{
                      ...defaultStyle,
                      ...transitionStyles[state],
                    }}
                  />
                )}
              </Transition>

also I get this kind of error:

enter image description here

so what happens now it makes transition but not the way I want it. I want to make the transition on every change of image. maybe you guys can please help me? Thanks alot !

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is probable that you are using React.StrictMode. Here a work-around is presented by the removal of StrictMode, however, that should only be tried if all else fails.

The documentation proposes adding refs. Here we can see an example for it:

import React, { createRef, Component } from "react";

// ChildComponent uses React.forwardRef to obtain the ref passed to it
// and then forward it to the DOM div that it renders.
const ChildComponent = React.forwardRef((props, ref) =>
  <div ref={ref}>
    <span>{props.children}</span>
  </div>
);


class MyComponent extends Component {

  componentDidMount() {
      const node = this.childRef.current;
      /* Uses DOM node  */ 
  }

  childRef = createRef();

  render () {
    return (
        // Pass the created ref to ChildComponent
        <ChildComponent ref={this.childRef}>{this.props.children}</ChildComponent>
    );
  }
}

export default MyComponent;

So, we:

  • import createRef
  • instantiate it
  • add a ref attribute to the component's markup pointing to this.childRef
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!