Students will build a simple form where they use the useRef
hook to focus an input field and display its current value.
import React, { useRef, useState } from 'react';
function RefForm() {
const [value, setValue] = useState("");
const handleFocus = () => {
// TODO: Use inputRef to focus the input field
};
const handleDisplayValue = () => {
// TODO: Use inputRef to display the current input field value
};
return (
<div>
<input type="text" placeholder="Type something..." />
<button onClick={handleFocus}>Focus Input</button>
<button onClick={handleDisplayValue}>Display Value</button>
<p>Current Value: {value}</p>
</div>
);
}
handleFocus
function to make the input field focused when the "Focus Input" button is clicked.handleDisplayValue
function to display the current value of the input field in the <p>
tag.