Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make MUI Fade fade in on render?

I'm using the Fade component of Material UI as part of a stepper. I want the steps to fade in on render. How can I make it work? sandbox

function getStepContent(step) {
  switch (step) {
    case 0:
      return (
        <Fade in>
          <div>'Step 1: Select campaign settings...'</div>
        </Fade>
      );
    case 1: ...
    default:
      return "Unknown step";
  }
}
like image 350
vuvu Avatar asked Dec 18 '25 10:12

vuvu


1 Answers

You could make a separate component for the Fade components, that would begin with the in prop set to false and then immediately set it to true using some state and a useEffect:

function CustomFade(props) {
  const [show, setShow] = useState(false);

  useEffect(() => {
    setShow(true);
  }, []);

  return <Fade in={show}>{props.children}</Fade>;
}

Also, you have to add the key prop to the CustomFade, so React remounts it.

function getStepContent(step) {
  switch (step) {
    case 0:
      return (
        <CustomFade key="step0">
          <div>'Step 1: Select campaign settings...'</div>
        </CustomFade>
      );
    case 1: ...
    default:
      return "Unknown step";
  }
}
like image 186
Adam Jeliński Avatar answered Dec 20 '25 01:12

Adam Jeliński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!