Skip to content

Instantly share code, notes, and snippets.

@siakaramalegos
Created June 2, 2017 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save siakaramalegos/342911778ccd50505d09c60d995c121a to your computer and use it in GitHub Desktop.
Save siakaramalegos/342911778ccd50505d09c60d995c121a to your computer and use it in GitHub Desktop.
Basic uncontrolled inputs form using refs
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