受控组件类似于vue中的数据双向绑定,可以将页面中输入类的控件绑定到state状态中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Input extends React.Component {
state = {
inputValue: ''
}
changeHandle = (e) => {
//这里可以拿到表单的值
this.setState({
inputValue: e.target.value
})
}
render() {
return (
<form action="" >
用户名:<input type="text" placeholder="请输入用户名" name="name" onChange={this.changeHandle} />
<button >登录</button>
</form>
)
}
}
ReactDOM.render(<Input />, document.getElementById("app"))
</script>
</body>
</html>