App
component code from which we will start today's experiments: import React, {Component} from "react" class App extends Component { constructor() { super() this.state = { firstName: "", lastName: "" } this.handleChange = this.handleChange.bind(this) } handleChange(event) { const {name, value} = event.target this.setState({ [name]: value }) } render() { return ( <form> <input type="text" value={this.state.firstName} name="firstName" placeholder="First Name" onChange={this.handleChange} /> <br /> <input type="text" value={this.state.lastName} name="lastName" placeholder="Last Name" onChange={this.handleChange} /> { /** * : * * <textarea /> * <input type="checkbox" /> * <input type="radio" /> * <select> <option> */ } <h1>{this.state.firstName} {this.state.lastName}</h1> </form> ) } } export default App
textarea
element. It’s probably the easiest to understand how to work with him. If you have previously used ordinary HTML forms, have already used this element, you know that this is not a self-closing tag, as was the case with the input
element. He has opening and closing parts. <br /> <textarea></textarea>
rows
and cols
you can specify its size when describing this element. In plain HTML, if it is necessary that after the output of this field some text already exists in it, this is done by entering the necessary text between the opening and closing tags of the element. In React, work with such elements is made as similar as possible to work with input
elements, which we talked about last time. Namely, in React, the textarea
tag is self-closing. That is, the code for displaying the field on the page can be changed as follows: <textarea />
value
attribute, and working with it is carried out in the same way as with the same attribute of ordinary text fields. Due to this, uniformity is achieved in working with different elements, and, in addition, it is easier to update the contents of the fields by updating the state properties associated with such fields. We give the state of the field code to this form: <textarea value={"Some default value"}/>
input
control, the type of which is a checkbox
. Here is its description: <input type="checkbox" />
value
attribute is not used when working with it. It is used in order to provide the user with a choice of some two options, one of which corresponds to the checkbox and the other to the unchecked. To track whether a checkbox is selected or cleared, the checked
attribute is used, which is described by a Boolean value. As a result, the flags usually correspond to logical properties stored in the state. this.state = { firstName: "", lastName: "", isFriendly: true }
<input type="checkbox" checked={this.state.isFriendly} />
isFriendly
property isFriendly
set to true
does not allow this. At the same time, a warning will be displayed in the console that we have not provided a mechanism for changing the field ( onChange
event onChange
) and it is displayed in the “read only” state.handleChange()
method. Now it is used to work with text fields. We will think about how to use it to work with the flag. To do this, first assign the above method as a handler for the onChange
event of the checkbox and assign a checkbox the name corresponding to the name of the state property associated with the checkbox. In addition, we will sign the checkbox using the label
tag: <label> <input type="checkbox" name="isFriendly" checked={this.state.isFriendly} onChange={this.handleChange} /> Is friendly? </label>
handleChange()
method, the code of which is shown below, we, when working with text fields, found out the name of the element ( name
) and its contents ( value
), and then updated the state by writing to it what the field with a specific name in its value
attribute: handleChange(event) { const {name, value} = event.target this.setState({ [name]: value }) }
value
attribute. It has only the checked
attribute, which can only be true
or false
. As a result, in order to use the handleChange()
method to work with a flag, we need to check whether the element for which this handler is called is a flag. To perform this check, recall that the type (element) of the input
element that represents the check box is set to checkbox
. To check this value, you can access the type
property of the event.target
element. Extract this property from the event.target
, as well as the property checked
, using the following construction: const {name, value, type, checked} = event.target
type
constant and find out whether the element for which the event handler is called is a flag. If this is the case, we will write to the state that turned out to be in the checked
constant. Don't forget to keep the code responsible for working with text fields. As a result, the handleChange()
code handleChange()
take the following form: handleChange(event) { const {name, value, type, checked} = event.target type === "checkbox" ? this.setState({ [name]: checked }) : this.setState({ [name]: value }) }
<textarea value={"Some default value"} onChange={this.handleChange} />
input
elements of the text
and checkbox
types. What is meant here is that the switches have both a value
attribute and a checked
attribute. Add a couple of switches to our form by creating their code based on the flag description code. Here's what it looks like: <label> <input type="radio" name="gender" value="male" checked={this.state.isFriendly} onChange={this.handleChange} /> Male </label> <br /> <label> <input type="radio" name="gender" value="female" checked={this.state.isFriendly} onChange={this.handleChange} /> Female </label>
gender
. Switches with the same name form a group. Selected can be only one switch included in such a group.checked
value is set, say, to true
, if a certain state property equals true
. Switches must support a synchronized change of their own state within a group. Instead, the checked
value of the switches is set by condition. In our case, this condition will be represented by comparing the state property this.state.gender
with the line male
or female
. In the switch description code, it looks like this: <label> <input type="radio" name="gender" value="male" checked={this.state.gender === "male"} onChange={this.handleChange} /> Male </label> <br /> <label> <input type="radio" name="gender" value="female" checked={this.state.gender === "female"} onChange={this.handleChange} /> Female </label>
gender
, initializing it with an empty string: this.state = { firstName: "", lastName: "", isFriendly: false, gender: "" }
<h2><font color="#3AC1EF">You are a {this.state.gender}</font></h2>
You are a
would not appear on it, but we will not do this, although you can easily implement it yourself. Now let's look at what we did. <select> <option></option> <option></option> <option></option> <select/>
value
attribute is used. This makes it easy to figure out exactly which element of the list is selected, and, in addition, it facilitates working with the state of the component.value
attribute of the select
element: value={this.state.favColor}
. This will get those values ​​that the user will choose. Now add favColor
to the state: this.state = { firstName: "", lastName: "", isFriendly: false, gender: "", favColor: "blue" }
onChange
event onChange
and give it a name. Also, assign the value values ​​to the options
elements of the combo box and enter the text that will be displayed in the field.select
element looks like with a caption: <label>Favorite Color:</label> <select value={this.state.favColor} onChange={this.handleChange} name="favColor" > <option value="blue">Blue</option> <option value="green">Green</option> <option value="red">Red</option> <option value="orange">Orange</option> <option value="yellow">Yellow</option> </select>
<h2><font color="#3AC1EF">Your favorite color is {this.state.favColor}</font></h2>
handleChange()
handler is that we have to handle flag events in a special way. <button>Submit</button>
button
element is found in the form, it will act as the old input
element with the type submit
. If this button is onSubmit
event of the onSubmit
form itself will be triggered. If you need to do something after completing the form, you can add an onClick
event onClick
to the button, but, for example, I personally prefer to handle such events at the form level, assigning an onSubmit
event handler to onSubmit
: <form onSubmit={this.handleSubmit}>
Source: https://habr.com/ru/post/444356/
All Articles