Advertisement
jerryturcios08

react-request-sample

Oct 23rd, 2019
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from "axios";
  2. import React from "react";
  3. import ReactDOM from "react-dom";
  4.  
  5. import "./styles.css";
  6.  
  7. class App extends React.Component {
  8.   state = {
  9.     username: "",
  10.     password: ""
  11.   };
  12.  
  13.   handleUsernameChange = e => {
  14.     this.setState({
  15.       username: e.target.value
  16.     });
  17.   };
  18.  
  19.   handlePasswordChange = e => {
  20.     this.setState({
  21.       password: e.target.value
  22.     });
  23.   };
  24.  
  25.   handleLoginSubmission = e => {
  26.     e.preventDefault();
  27.  
  28.     const data = {
  29.       username: this.state.username,
  30.       password: this.state.password
  31.     };
  32.  
  33.     axios
  34.       .post("http://gaillardia.cse356.compas.cs.stonybrook.edu/login", data)
  35.       .then(res => {
  36.         console.log(res);
  37.       })
  38.       .catch(err => {
  39.         console.log(err);
  40.       });
  41.   };
  42.  
  43.   render() {
  44.     return (
  45.       <div className="App">
  46.         <h1>Login</h1>
  47.         <form onSubmit={this.handleLoginSubmission}>
  48.           <div>
  49.             <label htmlFor="usernameInput">Username: </label>
  50.             <input
  51.               id="usernameInput"
  52.               onChange={this.handleUsernameChange}
  53.               type="text"
  54.               value={this.state.username}
  55.             />
  56.           </div>
  57.           <div>
  58.             <label htmlFor="passwordInput">Password: </label>
  59.             <input
  60.               id="passwordInput"
  61.               onChange={this.handlePasswordChange}
  62.               type="password"
  63.               value={this.state.password}
  64.             />
  65.           </div>
  66.           <button>Submit</button>
  67.         </form>
  68.       </div>
  69.     );
  70.   }
  71. }
  72.  
  73. const rootElement = document.getElementById("root");
  74. ReactDOM.render(<App />, rootElement);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement