1 October 2019

Get form data using ref attribute on form tag. This could be useful in some cases. The error that I was tackling before I found this solution: "Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.".

Source code viewer
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import {Container, Form} from 'semantic-ui-react';
  4.  
  5. class MyComponent extends React.PureComponent {
  6. constructor(props) {
  7. super(props);
  8. this.form = React.createRef();
  9. }
  10.  
  11. handleValidate(event) {
  12. event.preventDefault();
  13.  
  14. // Get form data using ref attribute on form tag.
  15. const formData = new FormData(ReactDOM.findDOMNode(this.form.current));
  16. }
  17.  
  18. render() {
  19. return <>
  20. <Container>
  21. <Form ref={this.form}>
  22. <Form.Button
  23. type="submit"
  24. onClick={e => this.handleValidate(e)>
  25. Validate
  26. </Form.Button>
  27. </Form>
  28. </Container>
  29. </>;
  30. }
  31. }
Programming Language: ECMAScript