reading-notes

React and Forms

React Docs - Forms

  1. What is a ‘Controlled Component’?

A controlled component is a component that renders a form as well as controls what happens when that form s manipulated further by user input. Any form elements that are in this way are called controlled components

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

we should update the state with their responses as soon as they enter them, this is because handle change runs on each keystroke to update the react state,with controlled components the inputs value is always driven by the react state.Essentially while that’s happening we could pass that value to other elements or reset it from other event handlers.

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

If we have an event handler on an input field we can use attribute names to each element to let the handler choose what to do based on the name or attribute given.

The conditional(Ternary) Operator

  1. Why would we use a ternary operator?

we would use the ternary operator to shorten down our if statements into one line of code. it shortens the entirety of the if statement between two options into one simple line of code.

  1. Rewrite the following statement using a ternary statement:
if(x===y){
  console.log(true);
} else {
  console.log(false);
}

We would write this using our ternary operator and turn it into

x === y ? true : false