Or for forms, if you're just submitting the values and not displaying them anywhere else, you can use uncontrolled inputs! :D
functionMyComponent(){consthandleSubmit=useCallback((evt)=>{evt.preventDefault()console.log(evt.target.elements.data1.value)// etc...// submit your data...},[])return<formonSubmit="handleSubmit"><inputname="data1"/><inputname="data2"/><inputname="data3"/><buttontype="submit">submit</button></form>}
Oh yes, definitely agree with the example and the point you're making! I just felt like throwing it out there as an extra gimmick, because a lot of people don't realize sometimes you don't need to use any state at all!
constsubmitHandler=event=>{event.preventDefault();constformData=newFormData(event.currentTarget);// You can send formData to the back-end using fetch.// It arrives there as an object { data1: value1, data2: value2, data3: value3 }};constMyComponent=()=>(<formonSubmit={submitHandler}><inputname="data1"/><inputname="data2"/><inputname="data3"/><buttontype="submit">submit</button></form>);
The good thing about using the platform that way, is that you can transfer that knowledge anywhere, not just React. You can literally use that same submitHandler in every library or framework out there (the philosophy that Remix has).
Software engineer, architect, consultant, amateur UI/UX designer, computer enthusiast, gamer. Often coming up with ideas and thoughts that I then write into a post within the next 60 minutes.
Or for forms, if you're just submitting the values and not displaying them anywhere else, you can use uncontrolled inputs! :D
Yes, but I needed an example and forms are the easiest one. =p
Oh yes, definitely agree with the example and the point you're making! I just felt like throwing it out there as an extra gimmick, because a lot of people don't realize sometimes you don't need to use any state at all!
It can be improved a little bit using
FormData:The good thing about using the platform that way, is that you can transfer that knowledge anywhere, not just React. You can literally use that same
submitHandlerin every library or framework out there (the philosophy that Remix has).Cheers!
I always prefer this over controlled inputs. Less code, better performance.