Hi folks! I know this is pretty simple but I can't figure it out, and I've tried doing what this article says but it didn't work.
https://reactgo.com/react-trigger-button-click/
This is my repo: https://github.com/andicuevasp/culinarian
Thanks in advance!!
#How to make button work with an onClick and Enter Key?
19 messages · Page 1 of 1 (latest)
When you tried doing what the article says, how exactly did you do it? I'm asking because your project seems a bit more complicated than the example in the article, since you have functions/event handlers being passed down as props. Also, do you have a deployed version of the site, so we can test things out?
You have several form's.
You don't do that
You have 1 form
And 1 onSubmit on the form not the button
As Barry stated, you are using form for each of your ingredients.
You shouldn't do that, it overcomplicates your form.
You should have 1 form with multiple inputs, then a button with a submit type. You can then attach a submit handler to the form, gather all your data from the submit event inside that handler, validate the data and send it to the API etc...
If you have a button with submit type and you are inside a form you can press enter as long as you are focused on any elements inside the form and that will submit the form. Since this is a button you can also click on the button which will submit the form.
That way you don't need to attach multiple handlers unnecessary to a button.
Hi Daniel! This is what I did:
async function getRecipes() {
const res = await fetch(
https://api.spoonacular.com/recipes/complexSearch?apiKey=${import.meta.env.VITE_CULINARIAN_APP_API_KEY}&includeIngredients=${ formData.firstIngredient },${formData.secondIngredient},${formData.thirdIngredient}&number=10&addRecipeInformation=true
);
const data = await res.json();
console.log(data)
setRecipeData(data.results);
setBeginSearch(true);
}
function handleKeypress(e) {
if (e.keyCode === 13) {
getRecipes();
}
}
and then passed handleKepress as a prop to ingredientsForm...
Reading @pulsar holly's answer, I see that I didn't do this form correctly at all! I never really learned forms properly so that's why I struggle wit this! Going back and doing what Bence mentioned 🙂
Trying this out now! Thank you so much!🙏
@pulsar holly Ok so I corrected my form and now have the following:
<form className='ingredients'> <h3>Ingredient 1</h3> <input className='ingredient-input' type='text' placeholder='' name='firstIngredient' onChange={props.handleChange} value={props.formData.firstIngredient} /> <h3>Ingredient 2</h3> <input className='ingredient-input' type='text' placeholder='' name='secondIngredient' onChange={props.handleChange} value={props.formData.secondIngredient} /> <h3>Ingredient 3</h3> <input className='ingredient-input' type='text' placeholder='' name='thirdIngredient' onChange={props.handleChange} value={props.formData.thirdIngredient} /> <button type='submit'>Let's cook!</button> </form>
I put my async getRecipes function inside the handleChange because I thought it would work, but whenever I start to type the first input, it already goes and fetches the API 😧 what am I doing wrong? This is what the handleChange now looks like:
`function handleChange(e) {
setFormData((prevFormData) => {
return {
...prevFormData,
[e.target.name]: e.target.value,
};
});
async function getRecipes() {
const res = await fetch(
`https://api.spoonacular.com/recipes/complexSearch?apiKey=${import.meta.env.VITE_CULINARIAN_APP_API_KEY}&includeIngredients=${
formData.firstIngredient
},${formData.secondIngredient},${formData.thirdIngredient}&number=10&addRecipeInformation=true`
);
const data = await res.json();
console.log(data)
setRecipeData(data.results);
setBeginSearch(true);
}
getRecipes()
}`
Ok I fixed it watching this video! https://www.youtube.com/watch?v=sRkv3Zz55-8
Thank you all anyways!!
how to post form data to fetch api react js - This video is tutorial about Post Form Data to Fetch API in React JS.
Github Codde
https://github.com/devopsdeveloper1107/React-practical/blob/main/Post Form Data to Fetch API with React JS
Well done making it work!
I am not familiar with your code base but I am not sure that is the correct approach.
If you could provide us a repo, we could take a look at the code and give you some code reviews
I ended up doing something else 😅 here’s the repo! https://github.com/andicuevasp/culinarian/tree/main/src
seems somewhat fine
im not a big fan of the
const recipeElement = props.recipeData.map((recipe) => {
return (
<li key={recipe.title}>
<a href={recipe.sourceUrl} target='_blank'>
<img src={recipe.image} />
<h4>{recipe.title}</h4>
</a>
</li>
);
});
thing here
but i cba finding out if it's the same problem as making components inside components
It looks like you've already gotten helped and have moved on, but thanks for sharing! Good luck with the rest of the project
I'll fork your repo and take a look