main
Rev | Line | |
---|
[c63036a] | 1 | import React, { createContext, useState, useEffect } from 'react';
|
---|
| 2 | import axios from 'axios';
|
---|
| 3 |
|
---|
| 4 | export const CustomerContext = createContext();
|
---|
| 5 |
|
---|
| 6 | export const CustomerProvider = ({children}) => {
|
---|
| 7 | const[customers, setCustomers] = useState([]);
|
---|
| 8 |
|
---|
| 9 | useEffect(() => {
|
---|
| 10 | const fetchCustomers = async () => {
|
---|
| 11 | try {
|
---|
| 12 | const response = await axios.get("http://localhost:8080/api/customers");
|
---|
| 13 | setCustomers(response.data)
|
---|
| 14 | } catch (error) {
|
---|
| 15 | console.log("Error fetching customers: ", error);
|
---|
| 16 | }
|
---|
| 17 | }
|
---|
| 18 | fetchCustomers()
|
---|
| 19 | }, []);
|
---|
| 20 |
|
---|
| 21 | return(
|
---|
| 22 | <CustomerContext.Provider value={{customers}}>
|
---|
| 23 | {children}
|
---|
| 24 | </CustomerContext.Provider>
|
---|
| 25 | )
|
---|
| 26 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.