Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hemepositive/ffe7c540b9483bdb3db5f0cfd16a412d to your computer and use it in GitHub Desktop.
Save hemepositive/ffe7c540b9483bdb3db5f0cfd16a412d to your computer and use it in GitHub Desktop.
Basic uncontrolled inputs using form serialization to get data from form
import React, { Component } from 'react'
// Import the serialize function from the form-serialize package
import serialize from 'form-serialize'
export default class UncontrolledSerialized 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 serialization, we just need to pass that function the
// serialized form body.
const form = e.currentTarget
const body = serialize(form, {hash: true, empty: true})
console.log(body)
}
render() {
// Let's see when re-renders happen.
console.log('render!')
return (
<form onSubmit={this.onSubmit}>
<h1>Uncontrolled Inputs + Form Serialization Example</h1>
<label>
Name
<input name="firstName" />
</label>
<label>
Spirit Animal
<input name="spiritAnimal" />
</label>
<button>Submit</button>
</form>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment