

When the browser loads a new HTML document, all existing JS variables and code is dumped.
=> Doesn't really matter to JS(with onde request, user instantly sees content on the screen)
If our React app followed traditional navigation ideas, It'd take more requests to show basic content.(requeest html, js, components)

JS ํ๊ฒฝ์์๋ Navigation ํ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฐ๋ค์ด ๋ฆฌ์
๋๋ค.
React๋ Navigation ํ ๊ฒฝ์ฐ ์ฃผ์ '/images'๋ก ๋ฐ๊พธ์ด์ ๋ชจ๋ compoment๋ฅผ ๋ฆฌ์
ํ ํ์์์ด ํด๋น ์ปดํฌ๋ํธ๋ง ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.

JS environment no longer being reset with navigation
This only works if the state is not defined in the component. It should be in the parent componet.

const NavigationContext = createContext();
function NavigationProvider({children}){
const [currentPath, setCurrentPath] = useState(window.location.pathname)
//// we can search the pathname to use console.log
useEffect(() =>{
const handler = () =>{
setCurrentPath(window.location.pathname);
}
window.addEventListener('popstate', handler);
return () =>{
window.removeEventListener('popstate', handler)
}
})
const navigate = (to) =>{
window.history.pushState({}, '', to);
setCurrentPath(to)
}
return (
<NavigationContext.Provider value={{currentPath, navigate}}>
{children}
</NavigationContext.Provider>
)
}
์ฌ์ฉ์์ ์ธ์
๊ธฐ๋ก ํ์์ผ๋ก ์ธํด ํ์ฌ ํ์ฑํ๋ ๊ธฐ๋ก ํญ๋ชฉ์ด ๋ฐ๋ ๋ ๋ฐ์ํฉ๋๋ค.
์ฃผ์์ ํจ๊ป ๋ฐ์ดํฐ๋ ์ ์ฅํ ์ ์๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด ๋ฐ์ดํฐ์ ๋ฐ๋ ํ์ด์ง์ ์ ๋ณด๋ค์ ๋ด์๋๊ณ ํด๋ผ์ด์ธํธ์์ ์ ๋ณด๋ฅผ ํ์ฉํด ์๋ก์ด ํ์ด์ง๋ฅผ ๋ ๋๋งํ๋ฉด ๋ฉ๋๋ค.
What part of it do we care about?
We only care about pathname "/" , "/images"
EX) google.com => pathname => "/"
window.location.pathname
// we can search the pathname to use console.log
window.history.pushState({}, "" , "/dropdown")
// third argument is new path we want to visit
Updates the address bar but doesn't cause a refresh
Do not full page refresh
history.pushState()
history.pushState์ ๊ฒฝ์ฐ ์๋ก์ด ์ฃผ์๋ชฉ๋ก์ ์ถ๊ฐ ํ๊ธฐ ๋๋ฌธ์ ์ด์ url์ ์ฃผ์๊ฐ ๋จ์์์ด ๋ธ๋ผ์ฐ์ ์ ๋ค๋ก๊ฐ๊ธฐ ๋ฒํผ์ด ํ์ฑํ ๋ฉ๋๋ค.
// ํ์ฌ url์ด test๋ผ๊ณ ๊ฐ์ ํ์ ๋
// ์ด์ url์ test, ํ์ฌ url์ test/pushpage๋ก ๋ณ๊ฒฝ๋๋ค.
history.pushState({ data: 'testData1' }, null, '/pushpage')
pushstate(state, title, url)
์ด๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์ธ์ ์คํ์ดํธ๋ฅผ ์คํ์ ์ถ๊ฐํ๋ค๊ณ ํ๋๋ฐ, ์ฝ๊ฒ ์ค๋ช ํ์๋ฉด ํด๋น url๋ก ์ด๋ํ ์ ์๊ฒ ํด์ฃผ๋ ์ญํ ์ ํ๋ค.
function Link({to}){
// Prop 'to' describes the path that user will go to if they click this
const handleClick = (e) =>{
e.preventDefault();
// Stops the standard navigation
console.log("User navigate to : ", to)
}
return <a onClick={handleClick} href={to}>Click</a>
//handleClick = Detect a click
}
User clicks forward or back?
=> Window emits a 'popstate' event if the user current url was added by'pushState'
=> Just type to url will refresh full page
window.history.pushState({}, "" , "/dropdown")
// do not full page refresh
//inside console
window.addEventListener("popstate", ()=> console.log(window.location.pathname))
// /aUrl when change to /aUrl
// /bUrl when change to /bUrl
const [currentPath, setCurrentPath] = useState(window.location.pathname);
// this is only for User clicks forward or back
// make do not refresh all the variables
useEffect(() => {
const handler = () => {
setCurrentPath(window.location.pathname);
};
window.addEventListener("popstate", handler);
return () => {
window.removeEventListener("popstate", handler);
};
}, []);

function Route({path, children}){
const { currentPath} = useNavigation();
if(path === currentPath){
return children
}
return null
}
//App.js
<Route path="/accordion">
<AccordionPage />
</Route>
<Route path="/dropdown">
<DropdownPage />
</Route>
function Link({to, children, className, activeClassName}){
const {navigate , currentPath} = useNavigation();
const classes = className(
'text-blue-500',
className,
currentPath === to && activeCCalssName
)
const handleClick =(e) =>{
if(e.metakey || e.ctrlKey){
//return ๋ช
๋ น๋ฌธ์ ํจ์ ์คํ์ ์ข
๋ฃํ๊ณ , ์ฃผ์ด์ง ๊ฐ์ ํจ์ ํธ์ถ ์ง์ ์ผ๋ก ๋ฐํํฉ๋๋ค.
return
}
e.preventDefault()
navigate(to)
}
return (
<a className={classes} href={to} onClick={handleClick}>
{children}
</a>
)
}
function Sidebar(){
const links =[
{ label: "Dropdown", path: "/" },
{ label: "Accordion", path: "/accordion" },
{ label: "Buttons", path: "/buttons" },
];
const renderedLinks = links.map((link)=>{
return (
<Link to={link.path}>{link.label}</Link>
)
})
return (
<div>{renderedLinks}</div>
)
}
Inside App.js
function App() {
return (
<div>
<Sidebar />
<div>