Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
how to put conditional function in react like if this state is active then onclick this otherwise this
import React, { Component } from 'react';
export class OverlayCart extends Component {
  constructor(){
    super();
    this.state={
      isCartActive:false,
    }
  }

  render() {
    // Function that will show the cart
  const handleCartActive = (e)=>{
    e.preventDefault();
    console.log("cart active");
    this.setState({
      isCartActive:!this.state.isCartActive
    })
  }
    // Function whose purpose of life to get the totalitems in the cart
    const getTotalItem = () =>{
      let totalItem = 0;
      this.props.cart.forEach(item => {
        totalItem += item.quantity;
      });
      return totalItem;
    }

    return (
      <NavbarItem >
        <NavbarItemIcon src="/assets/images/empty.png" alt="cart" onClick={(e)=>{handleCartActive(e)}}/>
                  {getTotalItem() ===0 ? "": <CartCount><span>{getTotalItem()}</span></CartCount>}
                  {this.state.isCartActive && <OverlayCartWrapperContainer toggleCart={handleCartActive}/> }
      </NavbarItem>

    )
  }
}


const  mapStateToProps = (state) =>{
  return {
      cart:state.cart.cart.cartItems
  }
}
export default connect(mapStateToProps)(OverlayCart);

I want isCartActive is true then on <NavbarItemIcon src="/assets/images/empty.png" alt="cart" onClick={(e)=>{handleCartActive(e)}}/> component does not have function on it if that is false then onClick={(e)=>{handleCartActive(e)}} this function should be there i am doing this because i have an event lister in another component when i click on this that eventListener runs the same function and one more time run because of the onClick if i can remove the onclick then the bug will be fixed

about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda