简体   繁体   English

为什么在使用create-react-app时表单未提交?

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

My google auth button was working fine before I migrated to using create-react-app. 在我迁移到使用create-react-app之前,我的Google身份验证按钮运行正常。 when I switch to CRA, the form is not being submitted to the server and instead it seems like the app is pushing the new path in the action attribute to the history. 当我切换到CRA时,该表单未提交给服务器,相反,该应用似乎将action属性中的新路径推送到了历史记录。 Does CRA automatically disables form submission somehow? CRA是否会以某种方式自动禁用表单提交? is there a work around? 周围有工作吗? Thanks! 谢谢!

this is the form: 形式如下:

<form method="get" action="/aoth/google" className="google-form">
      <input type="submit" className="google-login" value="Google+" />
</form>

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" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM