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

148
Visualizações
Why onClick event doesn't change the props.title in React

I wanna change the

title

by clicking the button but it doesn't change, can I have an explanation why is that happens?

import './ExpenseItem.css';
import ExpenseDate from './ExpenseDate';
import Card from './Card';

function ExpenseItem(props){
    let title  = props.expenseTitle;

    function clickedFunc(){
        title = "Update!";
    }
    return(
        <Card className='expense-item'>
            <ExpenseDate expenseDate={props.expenseDate}></ExpenseDate>
            <div className='expense-item__description'>
                <h2>{title}</h2>
                <div className='expense-item__price'>
                    &#8377;{props.expenseAmount}
                </div>
            </div>
            <button onClick={clickedFunc}>Click</button>
        </Card>
    );    
}

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

0

This is not how data is handled with React.

The title should be stored in a state variable (see useState).

Once the data is stored in a state variable, you will have to set it with setState. When setState is called in React, the component holding the state variable re-renders. This will in turn cause your ExpenseItem component to re-render because it is a child component of whatever higher level component passed it props.

In your parent component, you should see something like:

require { useState } from 'react';

const ParentComponent = (props) => {
   
   const [title, setTitle] = useState('Original Title');

   ...
   ...
   ...

   return (
      <div className="ParentComponent">
         <ExpenseItem
            title={title}
            setTitle={setTitle}
            expenseAmount={expenseAmount}
         />
      </div>
   )
}

Then, in your clickedFunc() function:

function clickedFunc() {
   props.setTitle("Update!");
}
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