why is form not submitting when using create-react-app?

后端 未结 1 1037
不知归路
不知归路 2021-01-26 01:18

My google auth button was working fine before I migrated to using create-react-app. when I switch to CRA, the form is not being submitted to the server and instead it seems like

相关标签:
1条回答
  • 2021-01-26 01:49

    Here's a simple snippet showing how to handle form submission. Hope that helps.

    class App extends React.Component {
      constructor() {
        super();
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    
      handleSubmit(event) {
        event.preventDefault();
        const form = event.target;
        const data = new FormData(form);
        for (let name of data.keys()) {
          const input = form.elements[name];
          console.log(input);
          console.log(input.value);
        }
        
        fetch('/aoth/google', {
          method: 'GET',
          body: data,
        });
      }
    
        render() {
          return (
            <form onSubmit={this.handleSubmit} className="google-form">
                  <input id="username" name="username" type="text" />
                  <input type="submit" className="google-login" value="Google+" />
            </form>
          );
       }
    }
    
    ReactDOM.render( < App / > ,
      document.getElementById('root')
    )
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    
    <div id="root" />

    0 讨论(0)
提交回复
热议问题