N
N
Nina2021-12-21 19:36:36
css
Nina, 2021-12-21 19:36:36

How to make two TransitionGroup in one React component?

Hello.
I'm learning React and trying to figure out the React Transition Group. I want to make it so that the number of ordered burgers and their amount are animated at the same time. But it does not work at the same time - either the quantity is animated, or only the amount. Tell me, is this possible? Here is the code:

import React from "react";
import Shipment from "./Shipment";
import { TransitionGroup, CSSTransition } from "react-transition-group";

class Order extends React.Component {

  renderOrder = (key) => {
    const burger = this.props.burgers[ key ];
    const count = this.props.order[ key ];
    const isAvailable = burger && burger.status === "available";
    const transitionsStyles = {
      classNames: "order",
      key,
      timeout: { enter: 500, exit: 500 }
    };

    if( isAvailable ) {
      return <CSSTransition
        { ...transitionsStyles }>
        <li key={ key }>
        <span>
          <TransitionGroup component="span" className="count">
            <CSSTransition classNames="count" key={ count } timeout={ { enter: 500, exit: 500 } }>
                <span>{ count } </span>
              </CSSTransition>
          </TransitionGroup>
            шт. { burger.name }
          <TransitionGroup component="span" className="price">
            <CSSTransition
              classNames="price"
              timeout={ { enter: 500, exit: 500 } }>
            <span> { count * burger.price } ₽</span>
            </CSSTransition>
    </TransitionGroup>
      <button className="cancellItem" onClick={ () => {
        this.props.deleteFromOrder( key );
      } }> &times;</button>;
    </span>
        </li>
      </CSSTransition>;

    }
    return <CSSTransition { ...transitionsStyles }      >
      <li className="unavailable" key={ key }>Извините, { burger ? burger.name : "бургер" } временно
        недоступен
      </li>
      ;
    </CSSTransition>;
  };

  render() {
    const orderIds = Object.keys( this.props.order );
    const total = orderIds.reduce( (prevTotal, key) => {
      const burger = this.props.burgers[ key ];
      const count = this.props.order[ key ];
      const isAvailable = burger && burger.status === "available";
      if( isAvailable ) {
        return prevTotal + burger.price * count;
      }
      return prevTotal;
    }, 0 );

    return (
      <div>
        <h2>Ваш заказ</h2>
        <TransitionGroup component="ul" className="order">
          { orderIds.map( this.renderOrder ) }
        </TransitionGroup>
        { total > 0 ? ( <Shipment total={ total }/> ) : (
          <div className="nothingSelected">Выберите блюдо и нажмите на кнопку 'Заказать'</div> ) }
      </div>
    );
  }
}

export default Order;


Link to github

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question