reactjs - What is the purpose of routers? - Stack Overflow

admin2025-04-20  0

I'm in the planning phase of building a web app. I keep seeing the concept of a "router" getting thrown around. And so far the reasoning has been:

  1. You need a router to get the back button
  2. You need a router to add navigation to your application
  3. Your users will be unhappy if you don't have a router

The problem I'm running into is that I'm not getting good explanations of the why. Why can't I just link directly to my webpage? I swear I've built apps where I have my index.html link to my_other_page.html and then been able to click the back button on my browser with everything working.

What am I missing?

I'm in the planning phase of building a web app. I keep seeing the concept of a "router" getting thrown around. And so far the reasoning has been:

  1. You need a router to get the back button
  2. You need a router to add navigation to your application
  3. Your users will be unhappy if you don't have a router

The problem I'm running into is that I'm not getting good explanations of the why. Why can't I just link directly to my webpage? I swear I've built apps where I have my index.html link to my_other_page.html and then been able to click the back button on my browser with everything working.

What am I missing?

Share Improve this question asked Mar 2 at 7:50 Visual StudioVisual Studio 3022 silver badges12 bronze badges 1
  • 4 React apps are usually SPAs (single page applications). They keep all their state in-memory in JavaScript objects. If your browser loads a different file, all state would be lost. – knittl Commented Mar 2 at 8:01
Add a comment  | 

1 Answer 1

Reset to default 1

Href, Hyperlinking, or hard navigation whatever you can call them. The notion of hard navigating pages like when you change from /home.html to /about-us.html, requires a full page refresh. You'll see a split-second blank white screen since the browser resets itself and start fetching the relevant data from a clean slate.

React Router works the opposite. Literally every page you visit (/, /about-us, or anything) is the same index.html. React Router fetch the whole website's information once and try to check what page you are trying to navigate and will try to optimize any rendering possible. You can call this as "soft navigation" since you are not actually changing the page but the React Router made you feel like you went to a different page.

This concept is called Single Page Application (SPA) since your website is literally a single page.

Try comparing between soft navigation and hard navigation and you'll see a clear difference.

Soft Navigation:

const navigate = useNavigate()

navigate("/page")

Hard Navigation:

windows.location.href("/page")
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745128108a286548.html

最新回复(0)