实例
1文本框 富文本框下拉框
2复选框
//导入react
import React from 'react'
import ReactDOM from 'react-dom'
//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值
class HelloWorld extends React.Component {
//初始化state
state = {
text: '1',
value:"2",
select:"nan",
checked:true
}
handleChange = (e) => {
this.setState({
txt: e.target.value,
})
}
handleChangeValue = (e) => {
this.setState({
value: e.target.value,
})
}
handleChangeSelect = (e) => {
this.setState({
value: e.target.select,
})
}
handleChangeCheck = (e) => {
this.setState({
checked: e.target.checked,
})
}
//抽离出来 this报错 事件处理中的this为underfine
render() {
return (
<div>
<input
type="text"
value={this.state.text}
onChange={this.handleChange}
/>
<textarea onChange={this.handleChangeValue} value={this.state.value}></textarea>
<select onChange={this.handleChangeSelect} value={this.state.select}>
<option value="nan">男</option>
<option value="nv">女</option>
</select>
<input
type="checkbox"
checked={this.state.checked}
onChange={this.handleChangeCheck}
/>
</div>
)
}
}
ReactDOM.render(<HelloWorld />, document.getElementById('root'))
运行结果
标签:checked,render,示例,36,value,表单,react,state,setState From: https://blog.51cto.com/u_15460007/6140115