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

107
Visualizações
How can I change State when an Event is triggered with useState?

to learn react im trying to implement a basic shop.

My Idea was to have many product-images. If an user clicks on an product-image this image turns around and shows something like comments, rating, etc of the product.

For this question i have 3 js Files: Container.js (contains everything from the product-cards to navbar etc), ProductList.js (returns the UL with all the different Products) and ItemCard.js (returns the actual product as LI ).

My Goal is to just invert the backsideVisible value.

I provide an minimal example for better understanding:

Container.js:

function Container() {
    const [item, setItem] = useState([{
        title: "some product title",
        price: "14.99$",
        backsideVisible: false
        id: 1
    }]);

    function handleTurn(event, itemId) {
        //here i want to change the backsideVisible value
        event.preventDefault();
        setItem(item.map(item => {
            if(item.id === itemId) {
                item.backsideVisible = !item.backsideVisible;
            }
        }))
    }

    return(
        <ProductList items={item} handleTurn={handleTurn}/>
    );
}

ProductList.js:

function ProductList(props) {
    return(
        <ul>
            <CardItem items={props.items} handleTurn={props.handleTurn} />
        </ul>
    );
}

CardItem.js

function CardItem(props) {
    return(
        {props.items.map(item =>(
            <li key={item.id} onClick={event => props.handleTurn(event, item.id)}>
                product-image etc...
            </li>
        ))}
        
    );
}

But everytime i try this, ill get an "TypeError: can't access property "id", item is undefined" error. As soon as i change the handleTurn Method to something like

function handleTurn(event, itemId) {
    event.preventDefault();
    console.log(itemId);
}

everything works fine and the console displays the id of the clicked item. So for me it seems, that my handleTurn Function has some errors.

Do you guys have any idea where my fault is?

Your help is much appreciated. Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're setting item (which should really be called items since it's an array. names matter) to an array of undefined elements, because your map() callback doesn't return anything:

setItem(item.map(item => {
    if(item.id === itemId) {
        item.backsideVisible = !item.backsideVisible;
    }
}))

Either return the updated object:

setItem(item.map(item => {
    if(item.id === itemId) {
        item.backsideVisible = !item.backsideVisible;
    }
    return item;
}))

or have the whole expression be a returned object:

setItem(item.map(item => ({
    ...item,
    backsideVisible: item.id === itemId ? !item.backsideVisible : item.backsideVisible
})));
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