1

In this component the div's been successfully entered the DOM with an animation. Now I want to implement an animation when I remove them from the DOM - but it not working. The idea is to add another class to the JSX elements, when it removed - but this add doesnt work for me. Any help will be appreciated. Here is my code:

import React, {Component} from 'react';
import PropTypes from 'prop-types';

import classes from './BurgerIngredient.css';

class BurgerIngredient extends Component {

  state={
      meat: <div className={classes.Meat}></div>,
      cheese: <div className={classes.Cheese}></div>,
      bacon: <div className={classes.Bacon}></div>,
      salad: <div className={classes.Salad}></div>
  }

  componentWillUnmount(){
//HERE I'M TRYING TO ADD '.removeBaconMeat' CLASS FOR THE 'MEAT' DIV, BUT IT'S NOW WORKING.
//THE GOAL WAS TO ADD'.removeBaconMeat' CLASS TO THE DIV SO IT WILL ADD ANOTHER ANIMATION WHEN THIS COMPONENT ( = DIV ELEMENT) IS UNMOUNTING (=REMOVED FROM DOM)
      if(this.props.type === 'meat'){
         const removedMeat = {...this.state.meat};
         removedMeat.props.className = removedMeat.props.className + ' ' + classes.removeBaconMeat;
         this.setState({meat: removedMeat});
      }
  }


  render(){
    let ingredient = null;
    switch(this.props.type){
        case ('bread-bottom'):
            ingredient = <div className={classes.BreadBottom}></div>;
            break;
        case ('bread-top'):
            ingredient = (
                <div className={classes.BreadTop}>
                    <div className={classes.Seeds1}></div>
                    <div className={classes.Seeds2}></div>
                </div>
            );
            break;
        case('meat'):
            ingredient = this.state.meat;
            break;
        case('cheese'):
            ingredient = this.state.cheese;
            break;
        case('bacon'):
            ingredient = this.state.bacon;
            break;
        case('salad'):
            ingredient = this.state.salad;
            break;
        default:
            ingredient = null;    
    }
    
    return ingredient;
    }
};

BurgerIngredient.propTypes = {
    type: PropTypes.string.isRequired
}

export default BurgerIngredient;
חיים חדד
  • 512
  • 5
  • 17
  • I'm not sure it will work. Since the component will be removed from the DOM tree. Instead of making the component unmount, I guess set its `display` to `none`. – Medi Apr 24 '21 at 11:43
  • 1
    Thats not the idea. My goal is first to add an CSS animation when I removing this component from the DOM. If its works when I create this component why not when I removing it? – חיים חדד Apr 24 '21 at 11:50
  • https://stackoverflow.com/questions/40064249/react-animate-mount-and-unmount-of-a-single-component – Medi Apr 24 '21 at 11:53
  • How CSS animation work on none existing elements in the DOM tree? You need to play your animation first before unmounting your element. – Medi Apr 24 '21 at 12:03
  • 1
    I understood its not working because the JSX element been removed already from the DOM, and the render method is not called again. So what are my options then? I need to apply one last animation before I removing this component. – חיים חדד Apr 24 '21 at 12:18

0 Answers0