Leaving component animation with the React Transition Group

Being new to React I have this question - I have two React components, a parent component:

class Container extends Component {

  render(){
    if(!this.props.someProps){
      return (
        <div className="container-text">
          <Intro />
        </div>
      )
    } else {
      return(
        <div className="container-text">
          Some text
        </div>
      )
    }
  }
}

      

The parent creates the child component based on some prop. Child component:

export default class Intro extends Component{

  constructor(props){
    super(props)

    this.state={
      animated: false
    }
  }

  componentDidMount(){
    this.setState({
      animated:true
    })
  }


  render(){
    return(
      <CSSTransition
        in={this.state.animated}
        timeout={1000}
      classNames='fade'>
        <div>
          <div className='intro-text'>
            <p>choose</p>
            <p>the</p>
            <p>situation</p>
          </div>
        </div>
      </CSSTransition>
    )
  }
}

      

I am getting a fade out animation when the component is initially mounted, but I don't understand how I can animate it when it leaves the DOM (so basically when this.props.someProps is true and only normal text is displayed in the container class)?

+3


source to share


1 answer


I wrote an article about this the other day. https://medium.com/@agm1984/how-to-manage-page-transition-animations-in-react-ba09c66655c6

Short answer: you can use a library react-transition-group

to add hooks to react-router

, which gives you control over the page exit event, which means you can do whatever you want in front of the component before disabling it.



You can do it with CSS or include a more powerful horsepower library like GSAP

.

If you are an ultra turbo user, you can browse the CodeSandbox repository for your article and see what you would be looking for: https://zw8n483zym.codesandbox.io/

0


source







All Articles