Skip to content

Instantly share code, notes, and snippets.

@LukeMwila
Last active June 7, 2018 14:01
Show Gist options
  • Save LukeMwila/4d8127796ee292eaf511586a6b18116f to your computer and use it in GitHub Desktop.
Save LukeMwila/4d8127796ee292eaf511586a6b18116f to your computer and use it in GitHub Desktop.
Understanding State in React
import React, { Component } from 'react'
class MyComponent extends Component{
state = {
superHero: ''
}
/** Update the state */
updateFavouriteSuperHero(e){
e.preventDefault()
this.setState({ superHero: e.target.value })
}
render(){
return(
<div>
My favourite super hero is {this.state.superHero}<br />
<input onChange={(e) => this.updateFavouriteSuperHero(e)} placeholder='Type favourite superhero' />
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment