Last active
January 30, 2019 16:14
-
-
Save dduleone/e729ad490dd1255df2ff0fba64a50488 to your computer and use it in GitHub Desktop.
Helping Mike
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import ListContacts from './ListContacts' | |
class App extends Component { | |
state = { | |
contacts: [ | |
{ id: 'Mike', | |
name: 'CampyandtheGoodVibes', | |
handle: '@CampyandtheGoodVibes' | |
}, | |
{ id: 'Jon', | |
name: 'Jonny Biscuits', | |
handle: '@JBandtheGravy' | |
}, | |
{ id: 'Lauren', | |
name: 'LaLa', | |
handle: '@InsaneintheUkraine' | |
}, | |
] | |
} | |
removeContact = (contact) => { | |
this.setState((currentState) => ({ | |
contacts: currentState.contacts.filter((c) => { | |
return c.id !== contact.id | |
}) | |
})) | |
} | |
render() { | |
return( | |
<div> | |
<ListContacts | |
contacts={this.state.contacts} | |
onDeleteContact={this.removeContact} | |
/> | |
</div> | |
) | |
} | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/*
From Mike: The code block I tried to highlight is what I would like a better explanation of, I think If I could more fully grasp what's happening with the removeContact Component I could more easily emulate it to create a component to add contacts and update contacts so I can start writing code with these concepts from scratch fluidly to build more involved applications.
*/
First
removeContact
is not a "component". It's a "function". (Components should start with capital letters.)Second, let's break down the function:
this.setState()
and passes a function as the parameter.currentState
parameter, and returns an object with acontacts
property.contacts
property is set based on expecting thecurrentState
object to have acontacts
property, which is expected to be an Array, and have thefilter
method applied to it.filter
method looks through each value in thecurrentState.contacts
array, and will return any items that do not match theid
value on thecontact
parameter that was passed intoremoveContact
.