gpt4 book ai didi

javascript - 使用 react-form POST 到 API?

转载 作者:行者123 更新时间:2023-11-30 09:30:08 25 4
gpt4 key购买 nike

可以找到 react-form 的文档 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;

最佳答案

您的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>
)
}
}

关于javascript - 使用 react-form POST 到 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46799872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com