📖 Scenario: You are building a simple React form where users can type their name. The form input should be controlled by React state to keep the input value in sync with the component.
🎯 Goal: Create a React functional component with a controlled text input. The input value should be stored in state and updated as the user types.
📋 What You'll Learn
Create a React functional component named NameForm
Initialize a state variable called name with an empty string
Add an <input> element of type text controlled by the name state
Update the name state on every input change using an onChange handler
💡 Why This Matters
🌍 Real World
Controlled components are used in React forms to keep the UI and data in sync, making it easier to validate and manage user input.
💼 Career
Understanding controlled components is essential for React developers building interactive forms and user interfaces.
Progress0 / 4 steps
1
Set up the React component and state
Create a React functional component called NameForm. Inside it, use the useState hook to create a state variable named name initialized to an empty string "".
React
Hint
Use const [name, setName] = useState("") inside the NameForm function.
2
Add the controlled input element
Inside the NameForm component, add an <input> element of type text. Set its value attribute to the name state variable.
React
Hint
Return an <input> element with value={name} inside the component.
3
Add the onChange handler to update state
Add an onChange event handler to the <input> element. The handler should update the name state using setName with the current input value from event.target.value.
React
Hint
Use onChange={event => setName(event.target.value)} on the input element.
4
Complete the component with a label and export
Wrap the <input> in a <label> with the text Name:. Also, add export default NameForm; at the end of the file.
React
Hint
Wrap the input inside <label> with text Name: and add export default NameForm; at the end.
Practice
(1/5)
1. What is a controlled component in React?
easy
A. A component that does not use state at all
B. A component that controls other components' rendering
C. A form input element whose value is controlled by React state
D. A component that uses refs to access DOM elements
Solution
Step 1: Understand controlled components
Controlled components are form inputs whose values are managed by React state, not by the DOM alone.
Step 2: Identify the correct definition
A form input element whose value is controlled by React state correctly describes this concept as React controlling the input value via state.
Final Answer:
A form input element whose value is controlled by React state -> Option C
Quick Check:
Controlled component = React state controls input value [OK]
Hint: Controlled means React state holds the input value [OK]
Common Mistakes:
Confusing controlled with uncontrolled components
Thinking refs control input value
Assuming controlled means no state usage
2. Which of the following is the correct way to create a controlled input in React?
easy
A.
B.
C.
D.
Solution
Step 1: Check controlled input syntax
A controlled input must have its value set from state and update state on change.
Step 2: Analyze options
<input value={this.state.text} onChange={this.handleChange} /> uses value from state and onChange handler to update it, which is correct.
Final Answer:
<input value={this.state.text} onChange={this.handleChange} /> -> Option A
Quick Check:
Value + onChange = controlled input [OK]
Hint: Controlled input needs value and onChange props [OK]
Common Mistakes:
Using defaultValue instead of value for controlled inputs
Omitting onChange handler
Not linking value to state
3. What will be the output when typing in this controlled input?
D. The onChange handler does not call setName with the new value
Solution
Step 1: Check onChange handler usage
The onChange handler is set to 'e => setName' which is a function reference, not a call with the new value.
Step 2: Correct onChange handler
It should be 'e => setName(e.target.value)' to update state with input value.
Final Answer:
The onChange handler does not call setName with the new value -> Option D
Quick Check:
onChange must call setState with event value [OK]
Hint: onChange must call setState with event target value [OK]
Common Mistakes:
Passing setName without calling it
Using defaultValue instead of value in controlled input
Forgetting to pass initial state to useState
5. You want to create a controlled textarea that converts all input to uppercase as the user types. Which code snippet correctly implements this?
hard
A. const [text, setText] = React.useState('');
return
B. const [text, setText] = React.useState('');
return
C. const [text, setText] = React.useState('');
return
D. const [text, setText] = React.useState('');
return
Solution
Step 1: Understand controlled textarea with uppercase transformation
The textarea's value must come from state, and onChange must update state with uppercase input.
Step 2: Analyze each option
const [text, setText] = React.useState('');
return <textarea value={text} onChange={e => setText(e.target.value.toUpperCase())} />; updates state with uppercase input on change and sets value from state, so textarea shows uppercase as typed.