npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-auto-page-router

v0.0.6

Published

**React Auto Page Router** is a lightweight library that provides Next.js-like automatic page routing for React applications. With this library, you can achieve seamless and dynamic routing with minimal configuration, leveraging Vite's import.meta.glob. T

Downloads

10

Readme

React Auto Page Router

React Auto Page Router is a lightweight library that provides Next.js-like automatic page routing for React applications. With this library, you can achieve seamless and dynamic routing with minimal configuration, leveraging Vite's import.meta.glob. This tool is perfect for developers looking for simplicity and efficiency in setting up their React application's routing.

Installation

Install the package via npm or yarn:

npm install react-auto-page-router

or

yarn add react-auto-page-router

Usage

To use the react-auto-page-router in your project, follow these steps:

1. Set Up Your File Structure

Organize your pages and layouts in the src/app directory. For example:

src/
  app/
    Home.page.tsx
    About.page.tsx
    Layout.layout.tsx
    blog/
      [id].page.tsx

2. Import and Use the AutoRouter Component

In your App.tsx file, use the import.meta.glob to dynamically import the page components and pass them to the AutoRouter component.

src/App.tsx:

import React from 'react';
import { AutoRouter } from 'react-auto-page-router';

const importPages = import.meta.glob('/src/app/**/*.{js,jsx,ts,tsx}');

const App = () => {
  return (
    <>
      <AutoRouter importedPages={importPages} />
    </>
  );
}

export default App;

3. Configure Routes Automatically

The AutoRouter component sets up the routes based on your file structure. It supports nested routes and dynamic routes using the [param] syntax.

Example Directory Structure:

src/
  app/
    Home.page.tsx
    About.page.tsx
    Layout.layout.tsx
    blog/
      [id].page.tsx

Routes Generated:

  • / -> Home.page.tsx

  • /about -> About.page.tsx

  • /blog/:id -> [id].page.tsx

Error Handling

The package includes an ErrorBoundary component that can be used to wrap your components for better error handling.

Example of ErrorBoundary:

import React from 'react';
import { ErrorBoundary } from 'react-auto-page-router';

const FallbackComponent = ({ error }: { error: Error }) => (
  <div>
    <p>Something went wrong:</p>
    <pre>{error.message}</pre>
  </div>
);

const MyComponent = () => {
  return (
    <ErrorBoundary fallback={<FallbackComponent />}>
      <SomeOtherComponent />
    </ErrorBoundary>
  );
};

export default MyComponent;

API

AutoRouter

The AutoRouter component is the core of the library, automatically configuring routes based on your file structure.

Props

  • importedPages: An object returned by import.meta.glob containing the dynamic imports for your page components.

ErrorBoundary

The ErrorBoundary component catches JavaScript errors in its child component tree and displays a fallback UI.

Props

  • fallback: A React node to display when an error is caught.

withErrorBoundary

A higher-order component (HOC) that wraps a component with an ErrorBoundary.

Usage

import React, { lazy } from 'react';
import { withErrorBoundary } from 'react-auto-page-router';

const SomeComponent = lazy(() => import('./SomeComponent'));

const WrappedComponent = withErrorBoundary(SomeComponent, <div>Failed to load component</div>);

export default WrappedComponent;

Vite-Specific Features

The import.meta.glob feature is specific to Vite. This library is optimized for use with Vite, which makes setting up dynamic routes extremely simple. If you're using another build tool or bundler, you might need to adapt the code or manually provide a similar glob functionality.

Simplified Autorouting

With react-auto-page-router, you can achieve Next.js-like routing in your React applications with minimal setup. Just organize your files, use the AutoRouter component, and enjoy automatic, dynamic routing without the hassle.

License

This project is licensed under the MIT License.