Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialbenjaminmosery
6,346 PointsThe names in my Application disappear when I click the 'Confirmed' Box?
Whenever I run my Application and click the 'Confirmed' checkbox, the names in my Application disappear. Here is what I am working with:
import React, { Component } from 'react';
import './App.css';
import Guestlist from './Guestlist';
class App extends Component {
state = {
guests: [
{
name: 'Julian',
isConfirmed: false
},
{
name: 'Alba',
isConfirmed: false
},
{
name: 'Ben',
isConfirmed: true
}
]
};
ToggleConfirmationAt = indextoChange =>
this.setState({
guests: this.state.guests.map((guest, index) => {
if (index === indextoChange) {
return {
isConfirmed:!guest.isConfirmed
};
}
return guest;
})
});
GetTotalInvited = () => this.state.guests.length;
//GetAttendingGuests = () =>
//GetUnconfirmedGuests = () =>
render() {
return (
<div className ="App">
<header>
<h1>RSVP</h1>
<p>A Treehouse App</p>
<form>
<input type="text" value="Safia" placeholder="Invite Someone"/>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</header>
<div className ="main">
<div>
<h2>Invitees</h2>
<label>
<input type="checkbox"/> Hide those who haven't responded
</label>
</div>
<table className ="counter">
<tbody>
<tr>
<td>Attending:</td>
<td>2</td>
</tr>
<tr>
<td>Unconfirmed:</td>
<td>1</td>
</tr>
<tr>
<td>Total:</td>
<td>3</td>
</tr>
</tbody>
</table>
<Guestlist
guests={this.state.guests}
ToggleConfirmationAt = {this.ToggleConfirmationAt}/>
</div>
</div>
);
}
}
export default App;
Any ideas as to why this is occurring? Any help would be appreciated.
1 Answer
jay aljoe
4,113 Pointsyou need to deconstruct the old object. what you doing is you are overwriting each object in the "guests" array with a new object that has only the "isConfirmed" property and not copying everything from the old obj. In the toggleConfirmationAt function, you need to deconstruct the "guests" property from the old state.
ToggleConfirmationAt = indextoChange =>
this.setState({
guests: this.state.guests.map((guest, index) => {
if (index === indextoChange) {
return {
...guest,
isConfirmed:!guest.isConfirmed
};
}
return guest;
})
});
or
toggelConfirmationAt = indexToChange =>(
this.setState({ guests:this.state.guests.map( (guest, index) => (
index === indexToChange ? {...guest, isConfirmed: !isConfirmed} :guest )
})
)