Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hemepositive/ffe7c540b9483bdb3db5f0cfd16a412d to your computer and use it in GitHub Desktop.

Select an option

Save hemepositive/ffe7c540b9483bdb3db5f0cfd16a412d to your computer and use it in GitHub Desktop.

Revisions

  1. @siakaramalegos siakaramalegos revised this gist Jun 2, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions UncontrolledSerializedForm.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    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 {
  2. @siakaramalegos siakaramalegos created this gist Jun 2, 2017.
    35 changes: 35 additions & 0 deletions UncontrolledSerializedForm.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import React, { Component } from 'react'
    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>
    )
    }
    }