Skip to main content

Command Palette

Search for a command to run...

Knowledge check: Forms in React - Advanced React

Updated
2 min read
Knowledge check: Forms in React - Advanced React
D

A passionate full-stack developer from @ePlus.DEV

  1. What of the next input types doesn’t have a controlled version when they are used in React?

    • <textarea />

    • <input type=”text” />

    • <input type=”file” />

  2. What are some of the features of controlled components? Select all that apply

    • Validating all values in the client side when a submission occurs in the form, before calling the server endpoint.

    • Enforcing a specific input format.

    • Conditionally disabling the submit button.

  3. How do you get the value of an input when its state is handled by the DOM (Uncontrolled)? Select all that apply.

    • Using a combination of useEffect and useRef hooks, where a ref is used on the uncontrolled input and then its value can be read on useEffect after a re-render cycle happens.

    • Using a ref via useRef hook, assigning it to the input and then reading the input value when the submission happens via ref.current.value.

    • Using local state and initializing it to an empty string. Then, reading the input value from the event object when the submission happens and finally setting the local state with that value.

  4. What happens when you click on the submit button in the below code snippet?

     <form onSubmit={() => alert("Submitting")}>
       <input type="text" value={text} onChange={e => setText(e.target.value)} />
       <input type="button" value="Submit" />
     </form>
    
    • The onSubmit callback is executed and an alert is shown with the text "Submitting".

    • An error is thrown.

    • Nothing happens when the button is clicked.

  5. What is missing in the below code for the select component to work properly?

     <select onChange={handleChange}>
       <option value="grapefruit">Grapefruit</option>
       <option value="lime">Lime</option>
       <option value="coconut">Coconut</option>
       <option value="mango">Mango</option>
      </select>
    
    • Each option tag should be accompanied by a label tag.

    • Each option tag should have an onChange handler.

    • The select tag is missing a value prop.


Tip & Tricks

Part 1 of 50

Quick and practical tips to help users optimize tasks, improve skills, and solve common problems effectively across various areas like tech, lifestyle, productivity, and more.