为了成功,你应该
反应思考
!
这意味着您应该始终记住,react组件在虚拟dom中起作用,处理表单提交的最可靠方法是将表单作为
反应组分
.
而不是使用
onLoad
您应该使用的事件
lifecycle
与虚拟dom对应的react中的事件。在这个例子中,我使用
componentDidMount
确定根组件在虚拟dom中装入(加载)的时刻。
class Form extends React.Component {
state = {
termUrl: 'http://www.url.gr/return.php',
md: 'NBGTEST000000412',
}
componentDidMount() {
this.props.onSubmit({
termUrl: this.state.termUrl,
md: this.state.md,
});
}
handleSubmit = (e) => {
e.preventDefault();
this.props.onSubmit({
termUrl: this.state.termUrl,
md: this.state.md,
});
}
render() {
return (
<form
name="threedfrom"
onSubmit={this.handleSubmit}
>
<h3>Form</h3>
<textarea style={{ display: 'none' }} name="PaReq"></textarea>
<span>*MεÏά Ïον ACS για Ïο capture ÏοÏ
PARes</span>
<input
type="hidden"
name="TermUrl"
value={this.state.termUrl}
/>
<input
type="hidden"
name="MD"
value={this.state.md}
/>
</form>
);
}
}
class App extends React.Component {
submit = (values) => {
console.log('Submitting', values);
const url = 'https://accreditation.datacash.com/acs-nbgs2a_i';
fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: values,
})
.then((resp) => { console.log('Got response', resp); })
.catch((err) => { console.error('Got error', err); })
}
render() {
return (
<div>
<h2>React App</h2>
<Form onSubmit={this.submit} />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('react'));
<html>
<head>
<title>Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
</head>
<body>
<div id="react"></div>
</body>
</html>