I am new to react and having problem trying to access data inside of the nested object. I am trying to create a app where the condition checks todays date with the date of the list and if the condition is true it returns the list.
The list looks like this :
const data = [{
id: 1,
name : 'Ashley',
birthday : {
year : 1998,
month: 5,
date : 22,
},
img : 'img'
},
{
id: 2,
name : 'Bill',
birthday : {
year : 1993,
month: 2,
date : 12,
},
img : 'img'
},
];
I have made a date object with which the comparision will be done.
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth()+1;
const date = today.getDate();
In the list, the birthday contains three other values and i can not access them using 'filter' method.
The rest of code are below :
function App() {
const [people, setPeople] = useState(data)
return (
<section className = 'container'>
<h1>{List.length} birthdays today</h1>
<List people= {people}/>
<button className="btn" onClick ={() => { setPeople([])}}>Clear All</button>
</section>
)
}
const List = ({people}) =>{
return (
<>
{ data.filter((person) => {
const {id, name, birthday, img} = person;
const age = year-person.birthday.year;
if(person.birthday.month === month && person.birthday.date === date){
return(
<article className= 'person' key = {id}>
<h2>{name}</h2>
<p>{age} years old</p>
<img src = {img} alt = "person"></img>
</article>
)}
return (
[]
)
})}
</>
)
}
Sorry about the long description.
Aucun commentaire:
Enregistrer un commentaire