Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react post request

componentDidMount() {
    // Simple POST request with a JSON body using fetch
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ title: 'React POST Request Example' })
    };
    fetch('https://jsonplaceholder.typicode.com/posts', requestOptions)
        .then(response => response.json())
        .then(data => this.setState({ postId: data.id }));
}
Comment

How to use Post Method in react

import axios from "axios";
import React from "react";

const baseURL = "https://jsonplaceholder.typicode.com/posts";

export default function App() {
  const [post, setPost] = React.useState(null);

  React.useEffect(() => {
    axios.get(`${baseURL}/1`).then((response) => {
      setPost(response.data);
    });
  }, []);

  function createPost() {
    axios
      .post(baseURL, {
        title: "Hello World!",
        body: "This is a new post."
      })
      .then((response) => {
        setPost(response.data);
      });
  }

  if (!post) return "No post!"

  return (
    <div>
      <h1>{post.title}</h1>
      <p>{post.body}</p>
      <button onClick={createPost}>Create Post</button>
    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: nuxt auth user info 
Javascript :: how to use javascript to hide content and show through link 
Javascript :: 10 javascript interview questions 
Javascript :: javascript check negative number 
Javascript :: date object js 
Javascript :: trim string and place ... javascript 
Javascript :: string length javascript 
Javascript :: javascript foreach loop array 
Javascript :: 1 line password strength checker jquery 
Javascript :: URLSearchParams for query params 
Javascript :: how to update state in react 
Javascript :: Run Express in Production Mode 
Javascript :: factors of a number 
Javascript :: usestate callback 
Javascript :: sails disable grunt 
Javascript :: set up emet for jsx in vs code 
Javascript :: how to calculate time taken for ajax call in javascript 
Javascript :: iconify react 
Javascript :: mdn destructuring 
Javascript :: how to find the radius of a loacation in node js 
Javascript :: git reset local branch to origin 
Javascript :: react extends component Increment data 
Javascript :: react state management 
Javascript :: js.l16 
Javascript :: sendmediagroup telegram nodejs 
Javascript :: angular material dialog close pass data 
Javascript :: angular javascript 
Javascript :: how to change owl nav, how to make custom next-prev button in owl carusol 
Javascript :: juqey off click 
Javascript :: how to hide api key in react app 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =