
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
@anansi/router
Advanced tools
A simple router designed for React 18 concurrent mode.
Parallel fetches
Even though all the data must be fetched - it appears instant because React delays rendering until the resources are available.
import { Controller, useController } from '@data-client/react';
import { createBrowserHistory } from 'history';
import { lazy, Route, RouteController, RouteProvider } from '@anansi/router';
const lazyPage = (pageName: string) =>
lazy(() => import(/* webpackChunkName: '[request]' */ `pages/${pageName}`));
const routes: Route<Controller>[] = [
{ name: 'home', component: lazyPage('Home') },
{
name: 'posts',
component: lazyPage('Posts'),
resolveData: async ({ fetch }: Controller) => fetch(PostResource.list(), {}),
},
];
const history = createBrowserHistory();
export const router = new RouteController({
history,
namedPaths,
routes,
notFound: { component: NotFound },
});
export function Router({ children }: { children: React.ReactNode }) {
const controller = useController();
return (
<RouteProvider
initialPath={globalThis.location.pathname}
router={router}
resolveWith={controller}
>
{children}
</RouteProvider>
);
}
interface Route<ResolveWith, Match = any> {
name: string;
component: React.ComponentType<any>;
resolveData?: (
resolveWith: ResolveWith,
match: Match & Route<ResolveWith, Match>,
) => Promise<void>;
}
Identifies the route
Component to render when this route matches
resolveData
is called when a location change occurs. Use this to prime your
networking cache with data needed for this route's components.
The resolution marks completion of a React concurrent transition.
The first argument passed will be whatevever you passed to the <RouteProvider/>
's resolveWith.
This helps with dispatchers whose lifetime is restricted to React.
Additional members can be defined and will be passed as props to the component.
Like React.lazy() but built for fetch-as-you-render as well as being memo'd.
Component will be rendered with props from the route match as well as any matching elements (like 'id' for /users/:id
)
Tracks and binds history to React. Place this in your top level provider.
Renders the currently matched route with the route passed as props. Place this in the body of your application below the <RouteProvider/>
This returns true when React is transitioning in Suspense. Use this to render a loading indicator in your application.
FAQs
React concurrent optimized router
The npm package @anansi/router receives a total of 439 weekly downloads. As such, @anansi/router popularity was classified as not popular.
We found that @anansi/router demonstrated a healthy version release cadence and project activity because the last version was released less than 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.