Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using react-form to POST to an API?

Docs for react-form can be found here. I'm having issues locating where, and how, a POST action to a URL is passed to the library. I have an API that is expecting the values of the form inputs, but I cannot seem to understand how I actually get the component to POST to an API endpoint specified by me.

Here is my form component:

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;
like image 825
Lukon Avatar asked Mar 07 '23 19:03

Lukon


2 Answers

Your render() method looks complicated. Try to keep logic outside of the render() method. A better approach to making a post request looks something like this:

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>
  )
 }
}
like image 106
Singh Avatar answered Mar 16 '23 15:03

Singh


I am pretty Happy with the fetch-api for doing http requests.

Example:

  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);
});

See reference: mozilla

Works like a charm with react. Nesting many requests with promises can be kind of annoying though.

like image 28
Mika Avatar answered Mar 16 '23 15:03

Mika