Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to login with api in react js

enter code here

import React, { useState } from "react";
import { Wrapper } from "./vehiclesTableStyles";
import { PostData } from "./postData";

function VehiclesTable() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");

const submitForm = e => {
e.preventDefault();

PostData(username, password).then(result => {
  console.log(result);
});
console.log("username", username);
console.log("password", password);
};
return (

<Wrapper>
  <div className="search_box">
    <form onSubmit={submitForm}>
      <input
        name="name"
        type="text"
        placeholder="username"
        onChange={e => setUsername(e.target.value)}
      />
      <input
        name="password"
        type="password"
        placeholder="search"
        onChange={e => setPassword(e.target.value)}
      />
      <input type="submit" value="login" />
    </form>
  </div>
</Wrapper>
);
}

export default VehiclesTable;



export function PostData(userData) {
let BaseUrl = "https://reqres.in//api/login";
console.log("userData", userData);
return new Promise((resolve, reject) => {
fetch(BaseUrl, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json"
  }
  // body: JSON.stringify(userData)
})
  .then(response => response.json())
  .then(responseJson => {
    resolve(responseJson);
  })
  .catch(error => {
    reject(error);
  });
});
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: $unset mongodb 
Javascript :: how to use jszip in node.js 
Javascript :: nodejs express server img src 
Javascript :: http delete angular 
Javascript :: webpack file-loader 
Javascript :: how to find out most repeated string in an array js 
Javascript :: Use ctrl + scroll to zoom the map & Move map with two fingers on mobile 
Javascript :: javascript rock paper scissors 
Javascript :: Cannot unpack array with string keys 
Javascript :: moment.js 
Javascript :: react useid hook 
Javascript :: jquery clone row 
Javascript :: angular socket.io with token header 
Javascript :: conditional jsx property 
Javascript :: how to add a picker in expo 
Javascript :: Send Form Data Using Ky AJAX 
Javascript :: console javascript 
Javascript :: innertext js 
Javascript :: json parse returns object 
Javascript :: Disabling right click using Javascript 
Javascript :: change the value in checkbox by button react 
Javascript :: sequelize left join attributes 
Javascript :: fatorial recursivo em javascript 
Javascript :: convert timestamp to utc javascript 
Javascript :: vue copy image to clipboard 
Javascript :: magento 2 translate js 
Javascript :: nodejs generate ethereum address 
Javascript :: javascript Program for Sum of the digits of a given number 
Javascript :: javascript foreach loop 
Javascript :: location.reload() js 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =