Implement react router
This commit is contained in:
		
							parent
							
								
									1d0c3f68c5
								
							
						
					
					
						commit
						d332635d6a
					
				
							
								
								
									
										10
									
								
								src/App.tsx
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/App.tsx
									
									
									
									
									
								
							@ -1,12 +1,14 @@
 | 
				
			|||||||
 | 
					import { BrowserRouter, Routes, Route } from 'react-router-dom';
 | 
				
			||||||
import './App.css';
 | 
					import './App.css';
 | 
				
			||||||
 | 
					 | 
				
			||||||
import Gallery from './Gallery/index';
 | 
					import Gallery from './Gallery/index';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function App() {
 | 
					function App() {
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <div className="App">
 | 
					    <BrowserRouter>
 | 
				
			||||||
      <Gallery />
 | 
					      <Routes>
 | 
				
			||||||
    </div>
 | 
					        <Route path="/photos/*" element={<Gallery />} />
 | 
				
			||||||
 | 
					      </Routes>
 | 
				
			||||||
 | 
					    </BrowserRouter>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,21 +1,34 @@
 | 
				
			|||||||
import React from 'react';
 | 
					 | 
				
			||||||
import '../App.css';
 | 
					import '../App.css';
 | 
				
			||||||
import placeholderPng from './placeholder.png';
 | 
					import placeholderPng from './placeholder.png';
 | 
				
			||||||
import { useState, useEffect } from 'react';
 | 
					import { useState, useEffect } from 'react';
 | 
				
			||||||
 | 
					import { useParams, useNavigate, useLocation } from 'react-router-dom';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function Gallery() {
 | 
					function Gallery() {
 | 
				
			||||||
 | 
					  const params = useParams<string>()['*'];
 | 
				
			||||||
 | 
					  const location = useLocation();
 | 
				
			||||||
 | 
					  const navigate = useNavigate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const [data, setData] = useState<galleryItemInfo[]>([]);
 | 
					  const [data, setData] = useState<galleryItemInfo[]>([]);
 | 
				
			||||||
  const [category, setCategory] = useState('');
 | 
					  const [category, setCategory] = useState(params === undefined ? '' : params);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
    post(category, (data: galleryItemInfo[]) => {
 | 
					    post(category, (data: galleryItemInfo[]) => {
 | 
				
			||||||
 | 
					      if (data.length === 0) {
 | 
				
			||||||
 | 
					        updateCategory('');
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      setData(data);
 | 
					      setData(data);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }, [category]);
 | 
					  }, [category]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    setData(getPlaceholderData());
 | 
				
			||||||
 | 
					    setCategory(params as string);
 | 
				
			||||||
 | 
					  }, [location]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const updateCategory = (category: string) => {
 | 
					  const updateCategory = (category: string) => {
 | 
				
			||||||
    setData(getPlaceholderData);
 | 
					    setData(getPlaceholderData());
 | 
				
			||||||
    setCategory(category);
 | 
					    setCategory(category);
 | 
				
			||||||
 | 
					    navigate(`/photos/${category}`);
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user