Created
June 2, 2017 18:21
-
-
Save siakaramalegos/342911778ccd50505d09c60d995c121a to your computer and use it in GitHub Desktop.
Basic uncontrolled inputs form using refs
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' | |
export default class UncontrolledRefsForm extends Component { | |
onSubmit = (e) => { | |
e.preventDefault() | |
// Usually, we would pass the final input values to a function that | |
// would do something with the data like persist it to a database. | |
// Using refs, we just need to pass the reference values. | |
console.log({ | |
firstName: this.firstName.value, | |
spiritAnimal: this.spiritAnimal.value, | |
}) | |
} | |
render() { | |
// Let's see when re-renders happen. | |
console.log('render!') | |
return ( | |
<form onSubmit={this.onSubmit}> | |
<h1>Controlled Inputs Example</h1> | |
<label> | |
Name | |
<input ref={(input) => this.firstName = input} /> | |
</label> | |
<label> | |
Spirit Animal | |
<input ref={(input) => this.spiritAnimal = input} /> | |
</label> | |
<button>Submit</button> | |
</form> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment