
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
react-router-tree
Advanced tools
Implement Next.js style page directories in your single page application. Supports React projects created using Vite or CRA (or any other webpack implementation).
Install the package using NPM
npm install react-router-tree
Pages are defined using a folder structure in your application. Subfolders denote path segments, while pages map to index.tsx or index.jsx files. In react-router-tree, each page is given it's own directory, in which assets and styles can be placed, allowing for a clean and structured page setup.
Parameters can be defined by using [name] as folder name. This will automatically translate into :name when building routes.
Folders named @ will map to the index page of the parent directory.
pages/
@/
index.tsx
styles.tsx
help/
index.tsx
style.scss
settings/
nested/
index.tsx
[param]/
index.tsx
The above example translates to the given routes
/
/help
/settings/nested
/settings/:param
Each page is expected to contain a default export of it's RouteObject. For TypeScript users a defineRoute helper function is included. The path property will automatically be populated when parsing the tree, so it may be omitted.
import { defineRoute } from "react-router-tree";
function IndexPage() {
return (
<div>
Page content
</div>
)
}
export default defineRoute({
element: <IndexPage />
});
// Using vite:
const pageTree: RouteTree = {
prefix: './pages',
routes: import.meta.glob('./pages/**/index.tsx', { eager: true })
};
// Using Create React App:
const pageTree: RouteTree = {
prefix: './',
routes: require.context('./pages/', true, /\index\.tsx$/)
};
// Combine page trees into a single array of routes
const routes = buildRouteObjects([pageTree]);
An example implementation can be found here.
react-router-tree is licensed under MIT
Copyright (c) 2022-present, Starlane Studios
FAQs
Implement Next.js style routing in any Vite or Webpack based react application
We found that react-router-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.