可以找到反应形式的文档here。我在查找将URL的POST操作传递到库的位置和方式时遇到问题。我有一个期望表单输入值的API,但我似乎无法理解我是如何实际将组件POST到我指定的API端点的。
这是我的表单组件:
import React, { Component } from 'react';
import { Form, Text, Select, Textarea } from 'react-form';
class DeploymentForm extends Component {
render() {
return (
<Form
onSubmit={(values) => {
console.log('Success!', values)
}}
validate={({ name }) => {
return {
name: !name ? 'A name is required' : undefined
}
}}
>
{({submitForm}) => {
return (
<div>
New STB Deployment
<form onSubmit={submitForm}>
<Text field='placeholder' placeholder='username'/>
<Text field='placeholder' placeholder='show'/>
<Text field='placeholder' placeholder='Git URL'/>
<Text field='placeholder' placeholder='Git Reference'/>
<Select
field='site'
options={[{
label: ''placeholder',
values: true
}]}
/>
<Select
field='Runway'
options={[{
label: 'Runway: stb',
values: true
}, {
label: 'Runway: stb2',
values: true
}, {
label: 'Runway: stb3',
values: true
}
]}
/>
<Select
field='Cluster: Default'
options={[{
label: 'placeholder',
values: true
}]}
/>
<Text field='hash' placeholder='placeholder' />
<Textarea
field='pre-deploy'
placeholder='placeholder'
<Textarea
field='post-deploy'
placeholder='placeholder'
/>
<Text field='placeholder' placeholder='placeholder'/>
<Text field='placeholder' placeholder='placeholder'/>
<button type='submit'>Submit</button>
</form>
</div>
)
}}
</Form>
)
}
}
export default DeploymentForm;
答案 0 :(得分:2)
您的render()
方法看起来很复杂。尝试将逻辑保留在render()
方法之外。制作帖子请求的更好方法如下所示:
class DeploymentForm extends Component {
constructor(props){
super(props);
this.state={'username': ''}
}
handleChange(e){
this.setState({username: e.target.value});
}
handleSubmit(e){
//call the api here with current state value (this.state.username)
}
render(){
return(
<form>
<input onChange={this.handleChange.bind(this)} type="text" name="username" placeholder="Enter name here" />
<button onClick={this.handleSubmit.bind(this)}>Submit</button>
</form>
)
}
}
答案 1 :(得分:0)
我很高兴使用fetch-api来执行http请求。
示例:
var myInit = { method: 'POST',
headers: {},
body: dataVar};
fetch('http://API.com', myInit).then(function(response) {
return response.json();
}).then(function(jsonResponse) {
//Success message
console.log(jsonResponse);
}).catch(error){
console.log(error);
});
参见参考:mozilla
像反应的魅力一样工作。使用promises嵌入许多请求可能会有点烦人。