reading-notes

Todd Wolden

View on GitHub

301 Class Four Reading Notes “React and Forms”

HTML form elements work a bit differently from other DOM elements in React, because form elements naturally keep some internal state. form has the default HTML form behavior of browsing to a new page when the user submits the form. If you want this behavior in React, it just works. But in most cases, it’s convenient to have a JavaScript function that handles the submission of the form and has access to the data that the user entered into the form. The standard way to achieve this is with a technique called “controlled components”.

Questions What is a ‘Controlled Component’? It is an input form element that gets its state from a single source by React so it can control the whole form.

Should we wait to store the users responses from the form into state when they submit the form OR should we update the state with their responses as soon as they enter them? Why. I didn’t find this answer but would wait to update until they submit the form.

How do we target what the user is entering if we have an event handler on an input field? You can use a name attribute on each input element and target it by using event.target.name

Why would we use a ternary operator? Use it for decision making because it is a shorter than an if statement.

Rewrite the following statement using a ternary statement: (x===y) ? console.log(true) : console.log(false);

Resources

Return to the Table of Contents

Table of Contents