chukwuyem

context

Oct 26th, 2020
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {createContext, useState} from 'react';
  2. import axios from 'axios';
  3.  
  4.  
  5. export const UserContext = createContext();
  6.  
  7. const UserContextProvider = (props) => {
  8.     const [Users, setUsers] = useState([
  9.        
  10.     ]);
  11.  
  12.     const addUser = async (email, password) => {
  13.     axios.post('http://127.0.0.1:8000/users/', { email,password}
  14.         .then(response => {
  15.           console.log(response)
  16.         })
  17.         .catch(error => {
  18.           console.log(error)
  19.         }))
  20.     }
  21.  
  22.     const getUsers = async () => {
  23.          axios.get(`http://127.0.0.1:8000/users/?limit=100`).then(response => {
  24.             setUsers (response.data)
  25.           })
  26.           .catch(error => {
  27.             console.log(error)
  28.           })
  29.       }
  30.  
  31.     return(
  32.         <UserContext.Provider value={{Users, addUser, getUsers}}>
  33.             {props.children}
  34.         </UserContext.Provider>
  35.     );
  36. }
  37.  
  38. export default UserContextProvider;
  39.  
  40.  
RAW Paste Data