0
Good evening, I’ve searched all the forum on the internet and I haven’t found anything to solve my problem. I’m studying React, and I’m trying to submit a form, but I don’t want the page to Reload when I submit the form, so I’ll use preventDefault(). But for some reason the function is not working, when I click the form button is sent normally. Follow the code:
function prevent(e) {
e.preventDefault();
}
return(
<>
<Template>
<Container>
<form onSubmit={(e) => prevent(e)}>
<FormField
label='Titulo:'
type='text'
placeholder='Titulo para a noticia'
value={values.title}
name='title'
onChange={handlerValue}
/>
<FormField
label='Link:'
type='text'
placeholder='Link para a noticia'
value={values.link}
name='link'
onChange={handlerValue}
/>
<button className='my-2'>Adicionar</button>
</form>
<ul>
{aux.map((item, indice) => {
return(
<li key={aux + indice}>
{aux}
</li>
)
})}
</ul>
</Container>
</Template>
</>
)
And why would that make a difference?
– Andre
What I understand is that by default the created function already receives the Event from where it is being called.
– Colasanto
In fact, it should perform in exactly the same way. This change only prevents the creation of an unnecessary anonymous function, but in no way changes the behavior of the component. https://codesandbox.io/s/bold-glitter-8lxbp?file=/src/App.js
– Rodrigo Amaral