This article brings you an introduction to the usage of React form elements (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Today we will talk about react form elements.
Controlled elements
Let’s take a look at how to get the value of the input box
import React, { Component } from 'react'; class Trem extends React.Component { constructor(props){ super(props); this.inp = this.inp.bind(this); this.sub = this.sub.bind(this); this.state = { place:"请输入...", inputV:'' } }; inp(e){ this.setState({ inputV:e.target.value {/* 通过事件细节改变inputV的值*/} }); console.log(e.target.value) }; sub(){ console.log(this.state.inputV) }; render(){ return ( <div> <input type="text" onChange={this.inp} placeholder={this.state.place} value={this.state.inputV}/> <br/> <button onClick={this.sub}>{this.props.main}</button>{/*此处的main是来自父组件的传值*/} </div> ) } } export default Trem;
Code interpretation:
this.inp = this. inp.bind(this); Bind the inp function this to point to
this.state Initialize the variable
inp() Monitor the input value of the input
this.state.inputV Obtain the value of the input through assignment
textarea tag
import React, { Component } from 'react'; class Trem extends React.Component { constructor(props){ super(props); this.inp = this.inp.bind(this); this.sub = this.sub.bind(this); this.state = { place:"请输入...", inputV:'' } }; inp(e){ this.setState({ inputV:e.target.value }); console.log(e.target.value) }; sub(){ console.log(this.state.inputV) }; render(){ return ( <div> <textarea type="text" onChange={this.inp} placeholder={this.state.place} value={this.state.inputV}/> <br/> <button onClick={this.sub}>{this.props.main}</button> </div> ) } } export default Trem;
Drop-down selection box
import React, { Component } from 'react'; class Trem extends React.Component { constructor(props){ super(props); this.select = this.select.bind(this); this.state = { selectV:'coconut' } }; select(e){ this.setState({ selectV:e.target.value }); console.log(e.target.value) }; render(){ return ( <div> <select value={this.state.selectV} onChange={this.select}> <option value="grapefruit">Grapefruit</option> <option value="lime">Lime</option> <option value="coconut">Coconut</option> <option value="mango">Mango</option> </select> <br/> </div> ) } } export default Trem;
Code interpretation:
Please note that the Coconut option is initially selected due to the selected attribute Chosen. In React, the previous selected attribute is not used, but the value attribute is used on the root select tag to represent the selected item. This is more convenient in controlled components, since you only need to update the component in one place.
In short, ,
Solution to multiple inputs
When you have multiple controlled input elements, you can add a name to each element Attribute to let the handler choose what to do based on the value of event.target.name.
import React,{Component} from 'react'; class More extends React.Component { constructor(props){ super(props); this.state = { isGoing: true, numberOfGuests: 2 }; this.handleInputChange = this.handleInputChange.bind(this); }; handleInputChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value }); console.log(event.target.checked,event.target.value) }; render(){ return ( <form> <label> Is going: <input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} /> </label> <br /> <label> Number of guests: <input name="numberOfGuests" type="number" value={this.state.numberOfGuests} onChange={this.handleInputChange} /> </label> </form> ) } } export default More;
Code interpretation:
this.setState({ });
es6 calculated attribute name syntax
Source code address: https://github.com/Nick091608...
[Related recommendations: react video tutorial】
The above is the detailed content of Introduction to the usage of React form elements (with code). For more information, please follow other related articles on the PHP Chinese website!