React 表单与事件 简单演示

前端开发 作者: 2024-08-19 20:55:01
import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import &
import React,{ Component } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';

class MyInput extends Component{
  constructor(props){
    super(props);
    this.state={
      text:'请输入内容'
    }
    this.textFn=this.textFn.bind(this);
  }

  textFn(e){
      .setState({
        text:e.target.value
      })
  }

  render(){
    return(
      <div>
        <input type="text" onChange={this.textFn} placeholder={this.state.text}/>
        <p>{this.state.text}</p>
      </div>
    )
  }
}

  ReactDOM.render(
    <div>
       <MyInput />
    </div>,document.getElementById('example')
  );

serviceWorker.unregister();
import React,1)">;

class Parent extends Component{
  constructor(props){
    super(props);
    (
      <Child textFn={this.textFn} text={this.state.text} />
    )
  }
}

class Child extends Component{
  render(){
    this.props.textFn} placeholder={this.props.text}/>
        <p>{this.props.text}</p>
      </div>
    )
  }
}

  ReactDOM.render(
    <div>
       <Parent />
    </div>,1)">)
  );

serviceWorker.unregister();
import React,1)">;

class Select extends Component{
  constructor(props){
    super(props);
    {
      text:''this.selectFn=this.selectFn.bind();
    this.submitFn=this.submitFn.bind();
  }

  selectFn(e){
      .setState({
        text:e.target.value
      })
  }

  submitFn(e){
    console.log('you choosed '+.state.text);
    e.preventDefault();
}

  render(){
    (
      <form onSubmit={this.submitFn}>
        <select value={this.state.text} onChange={this.selectFn}>
          <option value="a">a</option>
          <option value="b">b</option>
          <option value="c">c</option>
        </select>
        <input type="submit" value="提交" />
      </form>
    )
  }
}

  ReactDOM.render(
    <div>
       <Select />
    </div>,1)">)
  );

serviceWorker.unregister();
import React,1)">{
      text:true,num:4this.changeFn=this.changeFn.bind();
  }

  changeFn(e){
    const value=e.target.type==='checkBox' ? e.target.checked : e.target.value;
    const name=e.target.name;
      .setState({
        [name]:value
      })
  }

  render(){
    (
      <form>
        <input type="checkBox" name="text" checked={this.changeFn} />
        <br/>
        <input type="number" name="num" value={this.state.num} onChange={this.changeFn} />
      </form>
)
  );

serviceWorker.unregister();

总结

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65139.html