Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

And it makes it harder to do so. I hate doing forms in React. You basically have to reinvent the plumbing that's already standard with the HTML spec. Very tedious.


It literally doesn't. You can build a fully normal HTML form in react. The only reason to do any of it yourself is because you need something regular forms can't provide and even then you can still use as little or as much custom parts yourself.


It literally does, unless you have the form POST or GET to an API directly + a redirect from the API.

You have to override the submit functionality which is built-in by the browser. You have to maintain state of what the user inputs, which the browser handles for you. You have to reset inputs, which the browser handles for you (button type=reset). And a ton of people forget to actually validate on the server because they validate on the client.


You do have to override the submit functionality if you don't want to load a new page, sure, but that was always the case with plain Javascript or jQuery or whatever.

You don't have to maintain the state of what the user inputs if you leave the inputs as uncontrolled (https://reactjs.org/docs/uncontrolled-components.html) and access their value via a ref. You could even give them IDs and access their value directly via that if you wanted to, just like you would pre-React. But there are clearly advantages to making the inputs controlled by React, otherwise there'd be no reason to bother doing so (not saying that there aren't probaly a lot of cases where you could keep it simpler but people have been led to believe they need to put their form data in Redux so use some elaborate solution which doesn't scale, or whatever).

Also if you are doing forms in React, check out formik, it's the nicest library I've found so far. Granted it's not as "easy" as a straight HTML form that POSTs to a new page, of course, but modern web experiences often demand richer functionality than "the old way" allows.


Note that we specifically recommend that people _not_ put most form state in Redux:

https://redux.js.org/faq/organizing-state#should-i-put-form-...

Also, per the form libraries question: Formik and React Final Form are the two standard packages I suggest folks look at. React Hook Form seems to be gaining some popularity. Also, I recently ran across https://github.com/wsmd/react-use-form-state and tried it in one of my own projects, and was pretty impressed with it as a lightweight option.


It's sad how convoluted something as simple as form submission has become with "modern" Javascript frameworks. I'm not sure if it is React's fault, or the way most people use it, but it does seem to degrade into a mess of over-complexity. (I've been working on web sites since CGI scripts were a thing.)


Its as simple or as complex as you make it, you can use react to render a perfectly plain html form or you can manage it totally in react. And honestly, once you have everything set up and some previous examples, managing and submitting forms in react is trivial as well.


> And it makes it harder to do so. I hate doing forms in React. You basically have to reinvent the plumbing that's already standard with the HTML spec. Very tedious.

Show me the HTML standard that supports SPA submissions that don't reload the page.

It seems that your problem is about SPAs, not React.


>Show me the HTML standard that supports SPA submissions that don't reload the page.

Any HTML form that uses a standard button element rather than a submit button. Attach a JS event listener to it, read the form, post by AJAX.

That was a thing long before React came along.


That was a thing even before AJAX came along, eg via forms targetting an iframe.

A long time ago, I wrote a single-page chat application that continuously updated through chunked transfers, with submission via a plain old HTML form that received a 204 response.


You can actually do that with React. Nothing is stopping you.

It's not the recommended way because as soon as you want validation or dynamic fields you need state anyway.

And there're easy libraries like Formik that handle all the annoyances for you (although taking you away from raw HTML at the same time).

Do you miss punch-cards too?


Of course you can do that with React. GP implied that it was impossible without React, because standard HTML didn't support it, but wasn't correct.

There may be plenty of advantages to using React but it isn't necessary for "SPA submissions that don't reload the page."




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: