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

329
Visualizações
React onclick setState not working with arrow function

I have a component. I want to calculate some numbers here. When click the button it's not working with onClick={() => setEarned(calculateEarned)}. But when I change it like onClick={setEarned(calculateEarned)} it works but one time and when the page loaded. So I'm stack. Where is the problem I couldn't find.

import { Button, PrimaryButton } from "../../components/Button/Button"

export const CryptoCalculator = () => {
    const [earned, setEarned] = useState(0)

    function calculateEarned() {
        console.log('hit')
        return 1
    }

    return (
        <Container>
            <PrimaryButton
                title="Calculate"
                onClick={() => setEarned(calculateEarned)}
            ></PrimaryButton>

            <Earned>{earned}</Earned>
        </Container>
    )
}

EDIT: Problem solved. The problem is with the PrimaryButton component. I tried with button and then function worked. Here is the Button component:

import styled from "styled-components"

const Btn = styled.button`
    /*some css codes*/
`

const PrimaryBtn = styled(Btn)`
    /*some special css codes for primary button*/
`

export const Button = ({ title }) => {
    return <Btn>{title}</Btn>
}

export const PrimaryButton = ({ title }) => {
    return <PrimaryBtn>{title}</PrimaryBtn>
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to call your function correct way. setEarned(calculateEarned()) instead of setEarned(calculateEarned)

Example :

export const CryptoCalculator = () => {
    const [earned, setEarned] = useState(0)

    function calculateEarned() {
        console.log('hit')
        return 1
    }

    return (
        <Container>
            <PrimaryButton
                title="Calculate"
                onClick={() => setEarned(calculateEarned())}
            ></PrimaryButton>

            <Earned>{earned}</Earned>
        </Container>
    )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I've added onClick prop to Button component and then it started to act like a button. So, problem solved!

export const Button = ({ title, onClick }) => {
    return <Btn onClick={onClick}>{title}</Btn>
}

export const PrimaryButton = ({ title, onClick }) => {
    return <PrimaryBtn onClick={onClick}>{title}</PrimaryBtn>
}
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