Là một dự án mã nguồn mở, preactjs/preact-router đã đạt 1.1k sao trên GitHub, ngôn ngữ JavaScript. :earth_americas: URL router for Preact.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
[!WARNING]
preact-router unfortunately no longer sees active development! It's completely stable and so you can rely upon it for all existing apps, but for newer ones, we'd recommend using preact-iso for your routing needs instead. It offers a very similar API while integrating a bit better Suspense and lazy loading, with potentially more useful hooks. Thanks to all the contributors and users over the years!
Connect your Preact components up to that address bar.
preact-router provides a <Router /> component that conditionally renders its children when the URL matches their path. It also automatically wires up <a /> elements to the router.
💁 Note: This is not a preact-compatible version of React Router. preact-router is a simple URL wiring and does no orchestration for you.
If you're looking for more complex solutions like nested routes and view composition, react-router works great with preact as long as you alias in preact/compat.
See a Real-world Example :arrow_right:
Usage Example
import Router from 'preact-router';
import { h, render } from 'preact';
/** @jsx h */
const Main = () => (
<Router>
<Home path="/" />
<About path="/about" />
// Advanced is an optional query
<Search path="/search/:query/:advanced?" />
</Router>
);
render(<Main />, document.body);
If there is an error rendering the destination route, a 404 will be displayed.
Caveats
Because the path and default props are used by the router, it's best to avoid using those props for your component(s) as they will conflict.
Handling URLS
:information_desk_person: Pages are just regular components that get mounted when you navigate to a certain URL.
Any URL parameters get passed to the component as props.
Defining what component(s) to load for a given URL is easy and declarative.
Querystring and :parameter values are passed to the matched component as props.
Parameters can be made optional by adding a ?, or turned into a wildcard match by adding * (zero or more characters) or + (one or more characters):
It's possible to use alternative history bindings, like /#!/hash-history:
import { h } from 'preact';
import Router from 'preact-router';
import { createHashHistory } from 'history';
const Main = () => (
<Router history={createHashHistory()}>
<Home path="/" />
<About path="/about" />
<Search path="/search/:query" />
</Router>
);
render(<Main />, document.body);
Programmatically Triggering Route
It's possible to programmatically trigger a route to a page (like window.location = '/page-2')
import { route } from 'preact-router';
route('/page-2'); // appends a history entry
route('/page-3', true); // replaces the current history entry
Nested Routers
The <Router> is a self-contained component that renders based on the page URL. When nested a Router inside of another Router, the inner Router does not share or observe the outer's URL or matches. Instead, inner routes must include the full path to be matched against the page's URL:
import { h, render } from 'preact';
import Router from 'preact-router';
function Profile(props) {
// `props.rest` is the rest of the URL after "/profile/"
return (
<div>
<h1>Profile</h1>
<Router>
<MyProfile path="/profile/me" />
<UserProfile path="/profile/:user" />
</Router>
</div>
);
}
const MyProfile = () => <h2>My Profile</h2>;
const UserProfile = props => <h2>{props.user}</h2>;
function App() {
return (
<div>
<Router>
<Home path="/" />
<Profile path="/profile/:rest*" />
</Router>
<nav>
<a href="/">Home</a>
<a href="/profile/me">My Profile</a>
<a href="/profile/alice">Alice's Profile</a>
</nav>
</div>
);
}
render(<App />, document.body);
Route Component
Alternatively to adding the router props (path, default) directly to your component, you may want to use the Route component we provide instead. This tends to appease TypeScript, while still passing down the routing props into your component for use.
import { Router, Route } from 'preact-router';
function App() {
let users = getUsers();
return (
<Router>
<Route path="/" component={Home} />
{/* Route will accept any props of `component` type */}
<Route path="/users" component={Users} users={users} />
</Router>
);
}
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/preactjs/preact-router là nguồn chính thức.
preactjs/preact-router có phải mã nguồn mở không?
Có — preactjs/preact-router phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/preactjs/preact-router.
preactjs/preact-router có trang demo không?
Dự án có trang chủ ở http://npm.im/preact-router. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
preactjs/preact-router dùng license gì?
preactjs/preact-router phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
preactjs/preact-router là gì?
preactjs/preact-router (preactjs/preact-router) là dự án JavaScript trên GitHub. Theo mô tả gốc: :earth_americas: URL router for Preact.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc preact-router có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về preact-router.