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

115
Visualizações
How to animate a string prop of react component using mateiral ui transition?

I have trouble with animating the received prop from the parent component. The animation mechanism I use is Collapse component from material-ui.

When the parent component passes undefined as the text prop, the text disappears immediately and animation "jumps". Look at the code below:

import * as React from "react";
import Switch from "@mui/material/Switch";
import Collapse from "@mui/material/Collapse";
import FormControlLabel from "@mui/material/FormControlLabel";

interface AnimatedTextProps {
  text?: string;
}

const AnimatedText = ({ text }: AnimatedTextProps) => (
  <div>
    <Collapse in={Boolean(text)} timeout={2000} unmountOnExit>
      {/* when the parent component passes `undefined` as the text prop, the text disappears immediately */}
      {/* it causes that animation "jumps" */}
      {/* how to keep the text prop until the animation finishes? */}
      <div style={{ background: "red", padding: "40px" }}>{text}</div>
    </Collapse>
  </div>
);

export default function SimpleCollapse() {
  const [text, setText] = React.useState<string | undefined>(undefined);

  const handleChange = (e) => {
    const checked = e.target.checked;
    setText(checked ? "hidden text" : undefined);
  };

  return (
    <div>
      <FormControlLabel
        control={<Switch checked={Boolean(text)} onChange={handleChange} />}
        label="Show"
      />
      <AnimatedText text={text} />
    </div>
  );
}

Sandbox: https://codesandbox.io/s/simplecollapse-material-demo-forked-75tiv?file=/demo.tsx

How to prevent jumping and keep the text prop until the animation finishes?

about 4 years ago · Juan Pablo Isaza
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