Pineda
Pineda

Reputation: 7593

Is the use of the <form> tag necessary in ReactJS that have HOC input tags that handle form-esque events and behaviour?

The use of the form tag seems redundant especially in SPAs where the default action of refreshing the page is more of a hindrance than a feature: why include a tag only to have to write e.preventDefault() to prevent its default behaviour every time it is used?

Any form behaviour can be handled with a combination input tags, local component/application state and methods.

Form submission can be handled from handlers on inputs/controlled inputs themselves that in turn call some central form submission method.

I have tried this when creating a dynamic form component (inputs and types are built from an object dynamically) with no obvious downsides.

My question is: Given a situation where input tags are configured as HOCs whose events such as onChange, onClick and onKeydown handle the majority of events (like form submit's event like submit when the enter key is pressed) and whose value is controlled by state:
Are there any reasons as to why omitting the use of form tags to build a form within ReactJS is a bad idea?

Upvotes: 11

Views: 3775

Answers (1)

Andy Ray
Andy Ray

Reputation: 32066

Using a <form> tag gives you automatic form behavior, like form submission when pressing Enter when an input has focus. There is zero benefit from avoiding a form tag.

Upvotes: 11

Related Questions