React-3 PDF Ntclel
React-3 PDF Ntclel
agenda
input and controlled components
thinking in react
shopping cart application
InputBox Component
function InputBox() {
const [content, setContent] = useState("");
return (
<div>
<input type="text" value={content} onChange={handleChange} />
<button onClick={handleAlert}>Alert content</button>
</div>
);
}
Explanation
1. State Initialization:
The component uses the useState hook to create a state variable
content and a function setContent to update it.
const [content, setContent] = useState("");
Initially, content is an empty string.
2. Handling Input Change:
The handleChange function updates the state with the current value of the
input field.
const handleChange = (e) => { const updatedValue = e.target.value;
setContent(updatedValue); }
The onChange event on the input element triggers handleChange ,
ensuring the state content is updated with every keystroke.
3. Alert and Reset:
The handleAlert function alerts the current value of content and then
resets it to an empty string.
const handleAlert = () => { alert(content); setContent(""); }
This function is triggered by clicking the button .
4. Input Element:
The value attribute of the input element is set to content .
<input type="text" value={content} onChange={handleChange} />
This makes the input a controlled component because its value is
controlled by the state content .
Revision Notes: Shopping Cart Component
Step-by-Step Explanation
Begin by writing the static HTML structure. This includes a text input, a button,
and a placeholder for the list of tasks.
function Shopping() {
return (
<div className='Shopping-list'>
<div className="input-box">
<input type="text" />
<button>Add Item</button>
</div>
<h2>Shopping Cart</h2>
<ul className="list">
{/* List items will go here */}
</ul>
</div>
);
}
This static structure helps visualize the layout and plan for dynamic data
integration.
Step 2: Add Event Listener
Implement event listeners for the input field and button to handle user actions.
Code:
function Shopping() {
const [content, setContent] = useState("");
return (
<div className='Shopping-list'>
<div className="input-box">
<input type="text" onChange={handleInput} value={content}
/>
<button onClick={handleAddItem}>Add Item</button>
</div>
<h2>Shopping Cart</h2>
<ul className="list">
{/* List items will go here */}
</ul>
</div>
);
}
The handleInput function updates the state with the current input value.
The handleAddItem function will handle adding the new item to the list.
Use state to manage the input value ( content ) and the list of tasks ( tasks ).
Code:
function Shopping() {
const [content, setContent] = useState("");
const [tasks, setTasks] = useState([]);
return (
<div className='Shopping-list'>
<div className="input-box">
<input type="text" onChange={handleInput} value={content}
/>
<button onClick={handleAddItem}>Add Item</button>
</div>
<h2>Shopping Cart</h2>
<ul className="list">
{tasks.map((item, index) => (
<li key={index}>
<span>{item}</span>
<button>Delete</button>
</li>
))}
</ul>
</div>
);
}
Code:
function Shopping() {
const [content, setContent] = useState("");
const [tasks, setTasks] = useState([]);
return (
<div className='Shopping-list'>
<div className="input-box">
<input type="text" onChange={handleInput} value={content}
/>
<button onClick={handleAddItem}>Add Item</button>
</div>
<h2>Shopping Cart</h2>
<ul className="list">
{tasks.map((item, index) => (
<li key={index}>
<span>{item}</span>
<button onClick={() =>
handleDelete(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
handleDelete : Removes the item at the specified index from the list.
InputBox Component:
function InputBox({ handleInput, handleAddItem, content }) {
return (
<div className="input-box">
<input type="text" onChange={handleInput} value={content} />
<button onClick={handleAddItem}>Add Item</button>
</div>
);
}
ListItem Component:
function Shopping() {
const [content, setContent] = useState("");
const [tasks, setTasks] = useState([]);
return (
<div className='Shopping-list'>
<InputBox
handleInput={handleInput}
handleAddItem={handleAddItem}
content={content}
/>
<h2>Shopping Cart</h2>
<ListItem
tasks={tasks}
handleDelete={handleDelete}
/>
</div>
);
}