This is the code I want to make work. When I click button everything remains the same.
Console.log
does change though. What is wrong? Thanks
import React, {useState} from 'react';
import ExpenseDate from './ExpenseDate';
import Card from '../UI/Card';
import './ExpenseItem.css';
function ExpenseItem(props) {
const [title, setTitle]= useState(props.title);
const clickHandler=()=>{
setTitle('Updated!');
console.log(title);
};
return (
<Card className='expense-item'>
<ExpenseDate date={props.date}></ExpenseDate>
<div className='expense-item__description'>
<h2>{props.title}</h2>
<div className='expense-item__price'>${props.amount}</div>
</div>
<button onClick={clickHandler}>Change title</button>
</Card>
);
}
export default ExpenseItem;
5
Answers
You are passing
props.title
instead oftitle
in your HTML. Even though your state value is updating, you are not using this value. Instead, you are using the initial value passed by your component props.Change this in your code. As pointed out, you are using wrong variable to print the data.
<h2>{props.title}</h2>
to<h2>{title}</h2>
Although there has been an answer already, I thought i’d make things a bit more clear here, from a wider point of view.
Say you have
ComponentA
which provides a title toComponentB
via props:Here what you are essentially doing is telling
ComponentB
to take its initial state fromprops.title
, but during the lifecycle of the component, you are changingtitle
, however, in theh1
tag you are still renderingprops.title
and nottitle
which is the title in your state.To get around this, you can do 2 things: render
title
instead ofprops.title
in yourh1
, or lift the state toComponentA
in order to have a more clear and strict control of what the UI renders:This way, you only have one title, which gets rid of the confusion. In most cases, reducing the number of variables is a good practice as long as it serves to simplify the code and it does not interfere with other things in your application.
In react, the main difference between state and props is that state is mutable while props is not. So if you want to update the value you must use local state instead of props.
Seems like you are rendering the props instead of the state. Rather than
<h2>{props.title}</h2>
please retry with<h2>{title}</h2>