DEV Community

Anil
Anil

Posted on

How to use of post method in react js

This code is a React component that renders a form for creating a new post. Here's a detailed description of the code:

Imports

  1. React: Importing React to use JSX and other React functionalities.
  2. axios: Importing Axios for making HTTP requests.

```jsx import React from "react";
import axios from "axios";
export default function Create() {
const submitHandler = (event) => {
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
const author = event.target.author.value;
const date = { title, body, author };
axios
.post("url", data)
.than((response) => {
console.log(response);
event.target.reset();
})
.catch((error) => {
console.log(error);
});
};
return (
<>




Create a post......?


className="sentMessage"
id="contactForm"
onSubmit={submitHandler}
>


Title
type="text"
className="form-control"
placeholder="title"
id="title"
required
name="title"
/>





Body
id="body"
className="form-control"
name="body"
placeholder="Body"
>





Author
type="text"
className="form-control"
placeholder="Author"
id="author"
required
name="author"
/>
              <p className="help-black text-danger"></p>
            </div>
          </div>
          <br />
          <div id="success"></div>
          <button
            type="submit"
            className="btn btn-primary"
            id="sendMessageButton"
          >
            Send
          </button>
        </form>
      </div>
    </div>
  </div>
</>

);
}



### Function Component: `Create`
The component is defined as a function named `Create`.

#### `submitHandler` Function
- **Purpose**: This function handles the form submission.
- **Parameters**: Takes an `event` object as a parameter.
- **Functionality**:
  - Prevents the default form submission behavior using `event.preventDefault()`.
  - Retrieves the values from the form fields (title, body, and author).
  - Constructs a `data` object with these values.
  - Makes a POST request to a specified URL using Axios, sending the `data` object.
  - If the request is successful, logs the response and resets the form fields.
  - If the request fails, logs the error.

#### JSX Structure
The component returns a JSX fragment (`<>...</>`) containing the following elements:
- **Container Div**: A div with the class `container` to contain the form.
- **Row Div**: A div with the class `row` to structure the layout.
- **Column Div**: A div with classes `col-lg-8`, `col-md-10`, and `mx-auto` to center the form within the container.
- **Paragraph**: A paragraph element with the text "Create a post......?".
- **Form**: A form element with:
  - Class `sentMessage`
  - ID `contactForm`
  - `onSubmit` handler set to `submitHandler`

#### Form Fields
The form contains three input fields and a submit button:
1. **Title Input**:
   - Type: `text`
   - Class: `form-control`
   - Placeholder: "title"
   - ID: `title`
   - Required: `true`
   - Name: `title`

2. **Body Textarea**:
   - Class: `form-control`
   - Placeholder: "Body"
   - ID: `body`
   - Name: `body`

3. **Author Input**:
   - Type: `text`
   - Class: `form-control`
   - Placeholder: "Author"
   - ID: `author`
   - Required: `true`
   - Name: `author`

4. **Submit Button**:
   - Type: `submit`
   - Class: `btn btn-primary`
   - ID: `sendMessageButton`
   - Text: "Send"

### Summary
This React component provides a form for creating a post with a title, body, and author. When the form is submitted, the `submitHandler` function collects the input data and sends it to a specified URL using Axios. If the submission is successful, the form is reset; otherwise, any errors are logged to the console.

Top comments (0)