const log = (...value) => console.log(...value)
import ReactDOM from 'react-dom'
import React, { Component, PropTypes } from 'react'
class NameForm extends Component {
constructor (props) {
super(props);
}
handleSubmit (event) {
// this._name 返回当前节点
log(this._name)
event.preventDefault()
}
render () {
// 表单默认值 defaultValue 使用defaultValue 表示只控制初始值 不再控制后续更新
// ref 指定表单的ref值 通过this.refs.值 获取当前节点
// radio checkbox 默认值使用 defaultChecked
// text textarea select 默认值使用 defaultValue
// ref={input => this.变量 = input} 引用方式 this.变量
// 一次性检索 例如提交 建议使用 ref
return (
<form onSubmit={this.handleSubmit.bind(this)}>
<label>
<input type="text" ref={v => this._name = v} defaultValue="默认值" />
<input type="checkbox" defaultChecked="1" />
<select defaultValue="b">
<option value="a">1</option>
<option value="b">2</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
)
}
}
ReactDOM.render(
<NameForm />,
document.querySelector('#box')
)