
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-magic-router
Advanced tools

react-magic-router is a library designed to simplify and enhance the use of react-router-dom v6. It offers an easy setup process, type safety, and auto-completion for your routes, making your routing configuration clear and maintainable.
npm install react-magic-router
Add type and declare global type
import type { MappedGlobalRouter, RouteObjectMagic } from 'react-magic-router';
const PUBLIC_ROUTES = [
{
path: 'forgot-password',
element: <ForgotPasswordPage />,
query: 'email'
},
{
path: 'verify-otp',
element: <VerifyOtpPage />,
query: 'email&type' // <-- your query 'email,type'
},
{
path: ':id/profile', // <-- your param ':id'
element: <ProfilePage />,
}
// ... other routes
] as const satisfies RouteObjectMagic[]; // <-- Add this
export const routes = [
{
path: '/',
children: [
{
path: '/auth',
element: <PublicLayout />,
children: PUBLIC_ROUTES,
},
// ...other routes
],
},
] as const satisfies RouteObjectMagic[]; // <-- Add this
// This declaration type is required
declare module 'react-magic-router' {
type GlobalMagicRouter = MappedGlobalRouter<typeof routes>;
}
-------------- root app ----------------
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import {routes} from './your-path'
const router = createBrowserRouter(routes); // the same way with you setup react router v6
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>,
);
To navigate
import { useMagicRouter } from './path-to-your-hook';
function MyComponent() {
const { navigate } = useMagicRouter();
const handleNavigationToVerifyOtp = () => {
navigate({
path: '/auth/verify-otp', // <-- this will show autocomplete path for you choose
query: { email: '..@gmail.com',type:"..." } // <-- this type will take when you define query of path '/auth/verify-otp' is 'email&type' parse to { email: string , type: string } type
});
};
const handleNavigationToProfile =(id:string)=>{
navigate({
path: '/auth/:id/profile',
param: {
id
} // <--- this param will extract form '/auth/:id/profile' parse to { id: string } type
});
}
return (
<div>
...
</div>
);
}
To get param
import { useMagicRouter } from './path-to-your-hook';
function MyComponent() {
const { query } = useMagicRouter('/auth/verify-otp');
// query will have { email: string , type: string } type
const { params } = useMagicRouter('/auth/:id/profile');
// params will have { id: string } type
return (
<div>
...
</div>
);
}
import type { RouteObject } from "react-router-dom";
// RouteObjectMagic is extra type of RouteObject
type RouteObjectMagic = {
children?: RouteObjectMagic[];
query?: string;
} & DeepReadonly<Omit<RouteObject, "children">>;
// query will is string but this require format
// query = 'email' -> { email: string}
// query = 'email&otp' -> { email: string , otp: string}
// query = 'email&otp&...' -> { email: string, otp: string, ...}
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License.
FAQs
React magic router support system type for react router dom v6
The npm package react-magic-router receives a total of 158 weekly downloads. As such, react-magic-router popularity was classified as not popular.
We found that react-magic-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.