Last active
July 9, 2018 12:57
-
-
Save quinn/b383070691bb9a32b675152aabdada41 to your computer and use it in GitHub Desktop.
Revisions
-
quinn revised this gist
Jul 9, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,7 @@ export class Form extends React.PureComponent { export const Input = ({ name }) => <FormContext.Consumer> {({ formState, update }) => <input onChange={({ target }) => update(name, target.value)} value={formState[name]} /> } </FormContext.Consumer> -
quinn revised this gist
Jul 9, 2018 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,9 +17,12 @@ export class Form extends React.PureComponent { })) } onSubmit = () => this.props.onSubmit(this.state.formState) render () { return <FormContext.Provider value={this.state}> <form onSubmit={this.onSubmit}> {this.props.children} </form> </FormContext.Provider> -
quinn created this gist
Jul 9, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ import React from 'react' const FormContext = React.createContext({ formState: {}, update: () => {}, }) export class Form extends React.PureComponent { state = { formState: {}, update: (name, value) => this.setState(state => ({ formState: { ...state.formState, [name]: value, } })) } render () { return <FormContext.Provider value={this.state}> <form onSubmit={this.props.onSubmit}> {this.props.children} </form> </FormContext.Provider> } } export const Input = ({ name }) => <FormContext.Consumer> {({ value, update }) => <input onChange={({ target }) => update(name, target.value)} /> } </FormContext.Consumer>