Skip to content

Instantly share code, notes, and snippets.

@mariocsantos
Last active January 13, 2021 14:16
Show Gist options
  • Save mariocsantos/73292f97d4ddb6a92bd55ced87f4327e to your computer and use it in GitHub Desktop.
Save mariocsantos/73292f97d4ddb6a92bd55ced87f4327e to your computer and use it in GitHub Desktop.
Favorite food with onSubmit prop
import React, { useState } from 'react';
function FavoriteFood({ onSubmit }) {
const [favoriteFood, setFavoriteFood] = useState('');
const handleChange = (event) => {
setFavoriteFood(event.target.value);
}
const handleSubmit = (event) => {
event.preventDefault();
onSubmit(favoriteFood);
};
return (
<form onSubmit={handleSubmit}>
<label htmlFor="favorite-food">Favorite food</label>
<input id="favorite-food" type="text" onChange={handleChange} value={favoriteFood} />
<button disabled={!favoriteFood}>Add Food</button>
</form>
);
}
export { FavoriteFood };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment