Last active
January 13, 2021 14:16
-
-
Save mariocsantos/73292f97d4ddb6a92bd55ced87f4327e to your computer and use it in GitHub Desktop.
Favorite food with onSubmit prop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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