The Principles Behind Front-end Routing #128
zhangyu1818
announced in
en
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Libraries like
react-routerfundamentally depend on a package calledhistory.historyis essentially a wrapper for the nativewindow.historyAPI.window.history API
window.historyprovides five methods:go: Navigate to a specific page; the parameter is a number.go(1)goes forward one page,go(-1)goes back one page.back: Equivalent togo(-1).forward: Equivalent togo(1).pushState: Adds a new history record.replaceState: Replaces the current history record.We mainly use
pushStateandreplaceState.pushState
The
pushStatemethod takes three arguments.stateobject.url, which is displayed in the browser's address bar in real-time.The state object can be accessed via
window.history.stateand defaults tonull.To demonstrate, open the browser's console on the current page and type
window.history.pushState({state:0},"","/page"). You'll notice the browser address changes to/page.Run
window.history.statein the console; you'll see{state: 0}.Run
window.history.back()to go back a page.replaceState
The key difference between
replaceStateandpushStateis thatreplaceStatereplaces the current history record.Open your console and type
window.history.replaceState({state:1},"","/replace"). You'll notice the browser address changes to/replace.Type
window.history.statein the console to retrieve the current{state: 1}.Type
window.history.back()to navigate to the previous page because the last one was replaced by us.Tracking History Changes
The browser provides a
popstateevent to listen to history changes. However, this cannot track changes made bypushStateorreplaceState, nor can it determine the direction of navigation (forward or backward). It tracks only changes viago,back,forward, and browser navigation buttons.A Brief Dive into history's Source Code
The history library solves the limitations of native listeners. It unifies these various APIs into a single
historyobject and independently implements listener functionality. When callingpushorreplace, it triggers the associated event callback functions and passes in the direction of navigation.You'll notice that it merely creates its own
listenersarray and manually invokes them duringpushandreplace, thereby addressing the issues of native APIs not triggering these events.createHashHistoryis almost identical tocreateBrowserHistory, but it additionally listens forhashchangeevents.Implementing React Router from Scratch
Based on these principles, we can already write a simple router.
Below is a straightforward 20-line implementation example.
In essence, different
pathnameare used to display different elements. However,react-routerincludes more complex conditions and logic. A more detailed analysis of its source code will be published soon.Beta Was this translation helpful? Give feedback.
All reactions