Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-router-tree
Advanced tools
Implement Next.js style routing in any Vite or Webpack based react application
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.
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 />
});
Parameters can be defined by using [name]
as folder name. This will automatically translate into :name
when building routes.
We also support a other parameter type: ...
which will match any path segment.
Folders named @
will map to the index page of the parent directory.
You can add a parent pages by making a folder named _
anywhere in your file tree. Parent pages are responsible for placing an <Outlet />
where child routes will be rendered. Routes can be parented any number of times, each rendering in its closest parent outlet.
Visual example:
/example/_ - Responsible for rendering outlet 1
/example/@ - Rendered inside outlet 1
/example/page/_ - Rendered inside outlet 1 and responsible for rendering outlet 2
/example/page/@ - Rendered inside outlet 2
/example/page/child - Rendered inside outlet 2
The route object returned from a page allows defining a list of alternative routes that can be used to reach the page. These alternative paths are relative to the route itself and share the same meta.
Example:
export default defineRoute({
element: <HelpPage />,
alternatives: [
{ path: 'instructions' },
{ path: 'sub/path/:param' }
]
});
pages/
@/
index.tsx
styles.tsx
help/
index.tsx
style.scss
settings/
_/
index.tsx
overview/
index.tsx
[param]/
index.tsx
folder/
_/
index.tsx
[...]/
index.tsx
style.scss
The above example translates to the given routes
/
/help
/settings/overview
/settings/:param
/folder/*
// 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.
This package was originally developed for use in Vindigo, a free and open source task planner.
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
The npm package react-router-tree receives a total of 1 weekly downloads. As such, react-router-tree popularity was classified as not popular.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.