Accessing Data from a form REACT
Sat Jan 27 2024 18:00:23 GMT+0000 (Coordinated Universal Time)
Saved by
@Marcelluki
function Form() {
return (
<form>
<label>
Name:
<input type="text" />
</label>
<button type="submit">Submit</button>
<button type="button">Reset</button>
</form>
);
}
export default Form;
function Form() {
const [inputValue, setInputValue] = useState('');
const handleChange = (event) => {
setInputValue(event.target.value);
};
return (
<form>
<label>
Name:
<input type="text" value={inputValue} onChange={handleChange} />
</label>
<button type="submit">Submit</button>
<button type="button">Reset</button>
</form>
);
}
export default Form;
// NOW ADDING FORM SUBMISSION BUTTON HANDLER AND RESET BUTTON HANDLER
function Form() {
const [inputValue, setInputValue] = useState('');
const handleChange = (event) => {
setInputValue(event.target.value);
};
const handleSubmit = (event) => {
event.preventDefault();
console.log(inputValue);
};
const handleReset = () => {
setInputValue("");
};
return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input type="text" value={inputValue} onChange={handleChange} />
</label>
<button type="submit">Submit</button>
<button onClick={handleReset} type="button">Reset</button>
</form>
);
}
export default Form;
content_copyCOPY
https://tripleten.com/trainer/web/lesson/208423d5-830e-43fe-a2cd-b409bfe77455/task/1dfb3e8a-bac0-4598-a24c-c326da7d9ed3/
Comments