受控组件类似于vue中的数据双向绑定,可以将页面中输入类的控件绑定到state状态中

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <div id="app"></div>
    11. <script src="../js/react.development.js"></script>
    12. <script src="../js/react-dom.development.js"></script>
    13. <script src="../js/babel.min.js"></script>
    14. <script src="../js/prop-types.js"></script>
    15. <script type="text/babel">
    16. class Input extends React.Component {
    17. state = {
    18. inputValue: ''
    19. }
    20. changeHandle = (e) => {
    21. //这里可以拿到表单的值
    22. this.setState({
    23. inputValue: e.target.value
    24. })
    25. }
    26. render() {
    27. return (
    28. <form action="" >
    29. 用户名:<input type="text" placeholder="请输入用户名" name="name" onChange={this.changeHandle} />
    30. <button >登录</button>
    31. </form>
    32. )
    33. }
    34. }
    35. ReactDOM.render(<Input />, document.getElementById("app"))
    36. </script>
    37. </body>
    38. </html>