Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
How to change children on parent event?

I have a component divided on smaller components. On click on add button I need to remove selection from all active red children.

Selection is handling by className topping-false or topping-true (useState hook). So in fact I need to change all classnames to false if there any. How I can do it?

const Topping = (props) => {
const {
    img,
    name,
    price,
} = props

const [chosenTopping, setChosenTopping] = useState(false);

const handleTopping = () => {
    setChosenTopping(!chosenTopping)
    !chosenTopping ? store.dispatch(addTopping({...})) : 
                          store.dispatch(deleteTopping(name))
}

return <div className={'topping-' + chosenTopping} onClick={() => handleTopping()}>
    <img className='topping__image' src={img} alt={`${name}`} />
    <div className='topping__text'>
        <div className='topping__info'>
            <h3 className='topping__name'>{name}</h3>
            <h4 className='topping__price'>${price}</h4>
        </div>
    </div>
</div>;
};

export default Topping;

There is parent code:

const BigCard = (props) => {
const {
    img,
    name,
    price,
} = props

const newId = () => Math.floor((1 + Math.random()) * 0x10000000)
    .toString(16)

const addToCart = () => {
    store.dispatch(addDish({...}))
    store.dispatch(clearToppings())
}


return <div className='big-card'>
    <div className='big-card__content' onClick={(e) => handleBigCard(e)}>
        <img className='big-card__image' src={img} alt='pizza_image' />
        <div className='big-card__info'>
            <h1 className='big-card__name'>{name}</h1>
            <p className='big-card__description'>Chicken pieces, bell peppers, cheddar and parmesan cheese mix, mozzarella, red onion, sweet chili sauce, alfredo sauce</p>
            <Toppings />
            <div className='big-card__button'>
                <MenuButton onClick={() => addToCart()}>
                    <h3 className='big-card__price'>Add to cart for ${fullPrice}</h3>
                </MenuButton>
            </div>
        </div>
    </div>
</div>;
};

export default BigCard;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const BigCard = (props) => {
const {
    img,
    name,
    price,
} = props
const [isSelection,setSelection]=useState(true) //added
const newId = () => Math.floor((1 + Math.random()) * 0x10000000)
    .toString(16)

const addToCart = () => {
    setIsSelection((isSelection)=>!isSelection) //added
    store.dispatch(addDish({...}))
    store.dispatch(clearToppings())
}


return <div className='big-card'>
    <div className='big-card__content' onClick={(e) => handleBigCard(e)}>
        <img className='big-card__image' src={img} alt='pizza_image' />
        <div className='big-card__info'>
            <h1 className='big-card__name'>{name}</h1>
            <p className='big-card__description'>Chicken pieces, bell peppers, cheddar and parmesan cheese mix, mozzarella, red onion, sweet chili sauce, alfredo sauce</p>
            <Toppings isSelection={isSelection} />
            <div className='big-card__button'>
                <MenuButton onClick={() => addToCart()}>
                    <h3 className='big-card__price'>Add to cart for ${fullPrice}</h3>
                </MenuButton>
            </div>
        </div>
    </div>
</div>;
};

export default BigCard;

add isSelection with other props in the Toppings like so:

 const Toppings=({isSelection})=>{
 let array=[]
  return array.map(val=> <Topping isSelection={isSelection} />  ) 
}

then you can access the isSelection in the Topping


const Topping = (props) => {
const {
    img,
    name,
    price,
    isSelection

} = props
const [chosenTopping, setChosenTopping] = useState(false)
useEffect(()=>{
setChosenTopping(false)
}
,[isSelection])
.
..
..
...

return <div className={'topping-' + chosenTopping} onClick={() => handleTopping()}>
    <img className='topping__image' src={img} alt={`${name}`} />
    <div className='topping__text'>
        <div className='topping__info'>
            <h3 className='topping__name'>{name}</h3>
            <h4 className='topping__price'>${price}</h4>
        </div>
    </div>
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda