forked from mzabriskie/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path07-uncontrolled-form.js
More file actions
38 lines (33 loc) · 1.24 KB
/
07-uncontrolled-form.js
File metadata and controls
38 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, {Component} from 'react'
import PropTypes from 'prop-types'
// Pretty much every application is going to need to do something with forms
// There are two ways to handle forms elements with React.
// First, there's Uncontrolled inputs. They're arguably easier, but less powerful.
//
// The basic idea of uncontrolled components is you pull the value out of the DOM
// element when you need it. To do this, you need to get a reference to the element.
// You can either get a reference via an event handler `event` argument (`event.target`),
// or by using the special `ref` prop on the element like so:
//
// <input ref={node => this.input = node} />
//
// From there you can reference the input node elsewhere in your component methods.
//
// Exercise
// Render a form with an onSubmit handler that alerts the value of an input
//
// Tips:
// - You'll need an <input type="submit" value="Submit" />
// - Don't forget to do `event.preventDefault()` in your event handler
// - Bonus points for adding a defaultValue prop to the <input>
class NameForm extends Component {
render() {
return <div>Render a form here</div>
}
}
export const Example = () => <NameForm defaultName="Marcy" />
export default NameForm
/*
eslint
no-unused-vars: 0,
*/