-
Notifications
You must be signed in to change notification settings - Fork 2
07 Context
JP Barbosa edited this page Sep 24, 2022
·
10 revisions
code ./types/Item.ts
import { Rekognition, AWSError } from "aws-sdk";
export type Item = {
id: string;
createdAt: string;
labels?: Rekognition.DetectLabelsResponse;
error?: AWSError;
};
code ./frontend/src/contexts/AppContext.tsx
import React, { createContext, useContext, useState } from "react";
import { Item } from "../../../types/Item";
type AppContextInterface = {
uploadId?: string;
setUploadId: Function;
selectedItem?: Item;
setSelectedItem: Function;
};
const AppContext = createContext<AppContextInterface>({
setUploadId: () => {},
setSelectedItem: () => {},
});
type AppContextProviderProps = {
children: React.ReactNode;
};
export const AppContextProvider: React.FC<AppContextProviderProps> = ({
children,
}) => {
const [uploadId, setUploadId] = useState();
const [selectedItem, setSelectedItem] = useState<Item>();
return (
<AppContext.Provider
value={{ uploadId, setUploadId, selectedItem, setSelectedItem }}
>
{children}
</AppContext.Provider>
);
};
export const useAppContext = () => useContext(AppContext);
code ./frontend/src/App.tsx
import React from "react";
import { AppContextProvider } from "./contexts/AppContext";
import { Header } from "./components/Header";
function App() {
return (
<div id="app">
<AppContextProvider>
<Header />
<div className="content"></div>
</AppContextProvider>
</div>
);
}
export default App;
git add .
git commit -m "Context"