Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda