App.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import React, { ReactElement, useEffect, useState } from 'react';
  2. import {
  3. BrowserRouter as Router,
  4. Route,
  5. Switch
  6. } from 'react-router-dom';
  7. import { CaloriesList } from '../components/caloriesList/CaloriesList';
  8. import { AppContext } from './AppContext';
  9. import { PageFooter } from './pageFooter/PageFooter';
  10. import { PageHeader } from './pageHeader/PageHeader';
  11. import './app.css';
  12. const AppContextProvider = AppContext.Provider;
  13. const storedCalories = JSON.parse(localStorage.getItem('calorieItems') || '[]');
  14. function App (): ReactElement {
  15. const [calorieItems, setCalorieItems] = useState<CalorieItems>(storedCalories);
  16. const [selectedDate, setSelectedDate] = useState<number>(Date.now());
  17. useEffect(() => {
  18. localStorage.setItem('calorieItems', JSON.stringify(calorieItems));
  19. });
  20. return (
  21. <AppContextProvider value={{calorieItems, setCalorieItems, selectedDate, setSelectedDate}}>
  22. <PageHeader />
  23. <main id="pageContent">
  24. <Router>
  25. <Switch>
  26. <Route path="/">
  27. <CaloriesList />
  28. </Route>
  29. </Switch>
  30. </Router>
  31. </main>
  32. <PageFooter />
  33. </AppContextProvider>
  34. );
  35. }
  36. export {
  37. App
  38. };