Skip to content

Commit

Permalink
refactor: create useLocations hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yosevu committed Nov 4, 2022
1 parent b61635e commit d4cfe8f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/components/home/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { LocationCards } from './LocationCards';
import type { Location } from '../../types/sparkeats';
import { useLocations } from '../../useLocations';

export function HomePage() {
const locations = useLocations();

export function HomePage({ locations }: { locations: Location[] }) {
return (
<div className="homepage">
<header>Home Header</header>
Expand Down
3 changes: 1 addition & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/home/HomePage';
import { locations } from './locations';

window.__SPARKEATS_VERSION__ = import.meta.env['VITE_SPARKEATS_VERSION'];

Expand All @@ -12,7 +11,7 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<BrowserRouter basename={`${import.meta.env['BASE_URL']}`}>
<Routes>
<Route path="/" element={<App />}>
<Route path="/" element={<HomePage locations={locations} />} />
<Route path="/" element={<HomePage />} />
<Route path="/locations/:id" element={<div>Location Page</div>} />
<Route path="/locations/new" element={<div>New Location Page</div>} />
<Route path="/reviews/new" element={<div>New Review Page</div>} />
Expand Down
8 changes: 8 additions & 0 deletions src/useLocations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React, { useContext } from 'react';
import { locations } from './locations';

const LocationsContext = React.createContext(locations);

export const LocationsProvider = LocationsContext.Provider;

export const useLocations = () => useContext(LocationsContext);

0 comments on commit d4cfe8f

Please sign in to comment.