Challenge: Build a Controlled Form Component
Task
In this challenge, you will create a Form component with two input fields: email and password. The goal is to use the useRef hook to reference the input elements and handle form submission logic.
Instructions
- Import the
useRefhook from thereactlibrary. - Inside the
Formcomponent, declare twouseRefvariables:emailRefandpasswordRef, initialized withnull. - Reset the input fields by setting their values to an empty string using the
useRefvariables.
- To import the necessary hook from the React library, include anΒ
importΒ statement. - For this task, we will utilize theΒ
useRefΒ hook to store data without affecting the markup. - To determine the appropriate variable name for the ref, choose a word that is associated with the ref and add "Ref" at the end.
- To initialize the ref withΒ
null, passΒnullΒ as the initial value in the brackets of theΒuseRefΒ hook (e.g.,ΒuseRef(null)). - To reset the form input, use the appropriate ref variable and assign itsΒ
.current.valueΒ to an empty string ("").
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 5
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
Can you explain how to use useRef to reset the input fields?
What should the form submission handler look like?
Can you show how to connect the refs to the input elements?
Awesome!
Completion rate improved to 2.17
Challenge: Build a Controlled Form Component
Swipe to show menu
Task
In this challenge, you will create a Form component with two input fields: email and password. The goal is to use the useRef hook to reference the input elements and handle form submission logic.
Instructions
- Import the
useRefhook from thereactlibrary. - Inside the
Formcomponent, declare twouseRefvariables:emailRefandpasswordRef, initialized withnull. - Reset the input fields by setting their values to an empty string using the
useRefvariables.
- To import the necessary hook from the React library, include anΒ
importΒ statement. - For this task, we will utilize theΒ
useRefΒ hook to store data without affecting the markup. - To determine the appropriate variable name for the ref, choose a word that is associated with the ref and add "Ref" at the end.
- To initialize the ref withΒ
null, passΒnullΒ as the initial value in the brackets of theΒuseRefΒ hook (e.g.,ΒuseRef(null)). - To reset the form input, use the appropriate ref variable and assign itsΒ
.current.valueΒ to an empty string ("").
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 5