Skip to content

Latest commit

 

History

History
35 lines (20 loc) · 1.55 KB

301Read04.md

File metadata and controls

35 lines (20 loc) · 1.55 KB

Reading 04 -React and Forms

Source: React Docs

Q1 What is a ‘Controlled Component’?

A controlled component is an input form element, whose value is controlled by React, that renders a form and also controls what happens in that form on subsequent user input

Q2 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.

State should be updated as soon as the user enters their responses in the form, both to catch user errors earlier, and because it allows you to pass the value to other UI elements too, or reset it from other event handlers

Q3 How do we target what the user is entering if we have an event handler on an input field?

Create a function that will be called when the user enters or changes text in the input field. Then, inside the event handler, usd event.target.value to access the value of the input field. You can then update your component's state with this value.

Source: Medium

Q4 Why would we use a ternary operator?

A ternary operator can be used to shorten conditional statements.

Q5 Rewrite the following statement using a ternary statement:

if(x===y){
  console.log(true);
} else {
  console.log(false);
}

(x === y) ? console.log(true) : console.log(false)

Things I want to know more about

What does a controlled component look like in practice?