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

141
Visualizações
Set State inside method onClick

I am trying to set the state inside a method, i have a simple voting system (Yes or No) if you click Yes the state needs to be a string up and if you click No the state needs to be down

I am having trouble merging the method voteHandler and setVote

Here is my component :

import React, { useState, useContext } from 'react';
import {AnswerContext} from './AnswerWrapper';

const AnswerItem = (props) => {

    let indexPlus;

    const indexCount = (index) => {
        indexPlus = index;
        return indexPlus;
    }

    const { active, setActive } = useContext(AnswerContext)

    const [vote, setVote] = useState();

    const voteHandler = (e, index) => {
        e.preventDefault();
        setActive(index);
        setVote(""); // this might be "up" or "down"
        // I am sending this to a parent component
        props.onVote ({
            vote : vote,
            answerID : index
        })
    }

    return (
        <div>
            <button onClick={(e) => voteHandler(e, props.index)}>Yes <span>({props.ups !== null ? props.ups : 0 })</span></button>
            <button onClick={(e) => voteHandler(e, props.index)}>No <span>({props.downs !== null ? props.downs : 0 })</span></button>
        </div>
    )
}

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

0

You can send the value when you bind the onClick:

<button onClick={(e) => voteHandler(e, props.index, 'up')}>Yes <span>({props.ups !== null ? props.ups : 0 })</span></button>
<button onClick={(e) => voteHandler(e, props.index, 'down')}>No <span>({props.downs !== null ? props.downs : 0 })</span></button>

And use that in your voteHandler:

    const voteHandler = (e, index, value) => {
        e.preventDefault();
        setActive(index);
        setVote(value); // this might be "up" or "down"
        // I am sending this to a parent component
        props.onVote ({
            vote : vote,
            answerID : index
        })
    }

Warning

When you do:

        setVote(value); // this might be "up" or "down"
        // I am sending this to a parent component
        props.onVote ({
            vote : vote,
            answerID : index
        })

You will not get the updated vote since setState is asynchronous. If you want to call props.onVote on every vote and active change, use a useEffect:

const AnswerItem = (props) => {

    let indexPlus;

    const indexCount = (index) => {
        indexPlus = index;
        return indexPlus;
    }

    const { active, setActive } = useContext(AnswerContext)

    const [vote, setVote] = useState();
   
    useEffect(() => {
        props.onVote ({
            vote : vote,
            answerID : active
        })
    }, [vote, active])

    const voteHandler = (e, index) => {
        e.preventDefault();
        setActive(index);
        setVote("");
    }

    return (
        <div>
            <button onClick={(e) => voteHandler(e, props.index)}>Yes <span>({props.ups !== null ? props.ups : 0 })</span></button>
            <button onClick={(e) => voteHandler(e, props.index)}>No <span>({props.downs !== null ? props.downs : 0 })</span></button>
        </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