React - animate mount and unmount of a single component

前端 未结 16 1764
南笙
南笙 2020-12-22 16:34

Something this simple should be easily accomplished, yet I\'m pulling my hair out over how complicated it is.

All I want to do is animate the mounting & unmounti

16条回答
  •  一个人的身影
    2020-12-22 16:59

    I was also in dire need of single component Animation . I tired using React Motion but i was pulling my hairs for such a trivial issue.. (i thing). After some googling i came across this post on their git repo . Hope it helps someone..

    Referenced From & also the credit. This works for me as of now. My use case was a modal to animate and unmount in case of load and unload.

    class Example extends React.Component {
      constructor() {
        super();
        
        this.toggle = this.toggle.bind(this);
        this.onRest = this.onRest.bind(this);
    
        this.state = {
          open: true,
          animating: false,
        };
      }
      
      toggle() {
        this.setState({
          open: !this.state.open,
          animating: true,
        });
      }
      
      onRest() {
        this.setState({ animating: false });
      }
      
      render() {
        const { open, animating } = this.state;
        
        return (
          
    {(open || animating) && ( {(style => (
    ))} )}
    ); } }

提交回复
热议问题