Implement react router

This commit is contained in:
Sheldon Lee 2023-11-30 04:49:16 +08:00
parent 1d0c3f68c5
commit d332635d6a
2 changed files with 22 additions and 7 deletions

View File

@ -1,12 +1,14 @@
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import './App.css';
import Gallery from './Gallery/index';
function App() {
return (
<div className="App">
<Gallery />
</div>
<BrowserRouter>
<Routes>
<Route path="/photos/*" element={<Gallery />} />
</Routes>
</BrowserRouter>
);
}

View File

@ -1,21 +1,34 @@
import React from 'react';
import '../App.css';
import placeholderPng from './placeholder.png';
import { useState, useEffect } from 'react';
import { useParams, useNavigate, useLocation } from 'react-router-dom';
function Gallery() {
const params = useParams<string>()['*'];
const location = useLocation();
const navigate = useNavigate();
const [data, setData] = useState<galleryItemInfo[]>([]);
const [category, setCategory] = useState('');
const [category, setCategory] = useState(params === undefined ? '' : params);
useEffect(() => {
post(category, (data: galleryItemInfo[]) => {
if (data.length === 0) {
updateCategory('');
}
setData(data);
});
}, [category]);
useEffect(() => {
setData(getPlaceholderData());
setCategory(params as string);
}, [location]);
const updateCategory = (category: string) => {
setData(getPlaceholderData);
setData(getPlaceholderData());
setCategory(category);
navigate(`/photos/${category}`);
};
return (