You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
806 B

2 years ago
  1. import React from 'react'
  2. class Counter extends React.Component {
  3. // 1.声明用来控制 input value 的 react 组件自己的状态
  4. state = {
  5. message: 'this is message',
  6. }
  7. // 回调函数
  8. inputChange = (e) => {
  9. console.log('change事件触发了', e)
  10. // 4.拿到输入框最新的值,交给 state 中的 message
  11. this.setState({
  12. message: e.target.value,
  13. })
  14. }
  15. // 产出 UI 模板结构
  16. render() {
  17. return (
  18. // 2.给 input 框的 value 属性绑定 react state
  19. // 3.给 input 框绑定一个 change 的事件,为了拿到当前输入框中的数据
  20. <input
  21. type="text"
  22. value={this.state.message}
  23. onChange={this.inputChange}
  24. />
  25. )
  26. }
  27. }
  28. function App() {
  29. return <Counter></Counter>
  30. }
  31. export default App