Created
October 8, 2020 07:51
-
-
Save AnshKapoor/dff80b1f27e762e48c085d5b51dc4143 to your computer and use it in GitHub Desktop.
From Handling in React
This file contains 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' | |
class App extends Component{ | |
constructor(){ | |
super() | |
this.state = { | |
firstName : "", | |
lastName :"", | |
isFriendly:false, | |
defaultText:"This is textArea" | |
} | |
this.handleChange = this.handleChange.bind(this) | |
} | |
handleChange(event){ | |
const {name,value,type,checked} = event.target | |
type === "checkbox" ? this.setState({[name]:checked}) : this.setState({[name]:value}) | |
} | |
render(){ | |
return ( | |
<form> | |
<input type="text" | |
value = {this.state.firstName} | |
name = "firstName" | |
placeholder = "First Name" | |
onChange = {this.handleChange} | |
/> | |
<br/> | |
<input type="text" | |
value = {this.state.lastName} | |
name = "lastName" | |
placeholder = "Last Name" | |
onChange = {this.handleChange} | |
/> | |
<textarea value={"Some default value"} | |
onChange ={this.handleChange} | |
/> | |
<br/> | |
<label> | |
<input type = "checkbox" | |
name = "isFriendly" | |
checked = {this.state.isFriendly} | |
onChange = {this.handleChange} | |
/> Is Friendly | |
</label> | |
</form> | |
) | |
} | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment