Forms Flashcards

1
Q

Controlled Components & single source of truth

A

In HTML, form elements such as , , and typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components and only updated with setState().

We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a “controlled component”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example of controlled component & single source of truth

A

For example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component:

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);   }

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
alert(‘A name was submitted: ‘ + this.state.value);
event.preventDefault();
}

render() {
return (

      Name:

);   } }

Since the value attribute is set on our form element, the displayed value will always be this.state.value, making the React state the source of truth. Since handleChange runs on every keystroke to update the React state, the displayed value will update as the user types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Controlled component & React state

A

With a controlled component, the input’s value is always driven by the React state. While this means you have to type a bit more code, you can now pass the value to other UI elements too, or reset it from other event handlers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The textarea Tag

A

In HTML, a element defines its text by its children. In React, a uses a value attribute instead. This way, a form using a can be written very similarly to a form that uses a single-line input. Notice that this.state.value is initialized in the constructor, so that the text area starts off with some text in it:

class EssayForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ‘Please write an essay about your favorite DOM element.’
};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);   }

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
alert(‘An essay was submitted: ‘ + this.state.value);
event.preventDefault();
}

render() {
return (

      Essay:

);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The select Tag

A

React, instead of using the selected attribute, uses a value attribute on the root select tag. This is more convenient in a controlled component because you only need to update it in one place. For example:

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);   }

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
alert(‘Your favorite flavor is: ‘ + this.state.value);
event.preventDefault();
}

render() {
return (

      Pick your favorite flavor:

        Grapefruit
        Lime
        Coconut
        Mango

);   } }

Overall, this makes it so that , , and all work very similarly - they all accept a value attribute that you can use to implement a controlled component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The file input Tag

A

In HTML, an lets the user choose one or more files from their device storage to be uploaded to a server or manipulated by JavaScript via the File API.

Because its value is read-only, it is an uncontrolled component in React.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Handling Multiple Inputs

A

When you need to handle multiple controlled input elements, you can add a name attribute to each element and let the handler function choose what to do based on the value of event.target.name:

class Reservation 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
});   }

render() {
return (

      Is going:

    <br>

      Number of guests:

);   } }

Note how we used the ES6 computed property name syntax to update the state key corresponding to the given input name:

this.setState({
[name]: value
});

It is equivalent to this ES5 code:

var partialState = {};
partialState[name] = value;
this.setState(partialState);

Also, since setState() automatically merges a partial state into the current state, we only needed to call it with the changed parts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly