-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalState.tsx
More file actions
36 lines (29 loc) · 978 Bytes
/
GlobalState.tsx
File metadata and controls
36 lines (29 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { createContext, useContext, useState } from 'react';
// Define el tipo de dato para los escaneos
interface Scan {
id: string;
data: any; // Puedes ajustar el tipo según los datos que manejes
}
interface GlobalStateContextType {
scans: Scan[];
addScan: (scan: Scan) => void;
}
const GlobalStateContext = createContext<GlobalStateContextType | undefined>(undefined);
export const GlobalStateProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [scans, setScans] = useState<Scan[]>([]);
const addScan = (scan: Scan) => {
setScans((prevScans) => [...prevScans, scan]);
};
return (
<GlobalStateContext.Provider value={{ scans, addScan }}>
{children}
</GlobalStateContext.Provider>
);
};
export const useGlobalState = () => {
const context = useContext(GlobalStateContext);
if (!context) {
throw new Error('useGlobalState must be used within a GlobalStateProvider');
}
return context;
};