Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/penghuili/react-baby-router

👶Such a simple react router.
https://github.com/penghuili/react-baby-router

react react-router router

Last synced: 3 months ago
JSON representation

👶Such a simple react router.

Awesome Lists containing this project

README

        

# react-baby-router

Such a simple react router. I guess this is the simplest react router in the world.

## Limitations

- No path params, you have to use query params;
- No nested route;

## Installation

```
npm install react-baby-router
```

## Usage

### Structure your router:

```
import { BabyRoutes } from 'react-baby-router';
import React from 'react';

import { Account } from './views/Account.jsx';
import { Notes } from './views/Notes.jsx';
import { NoteDetails } from './views/NoteDetails.jsx';
import { SignUp } from './views/SignUp.jsx';
import { SignIn } from './views/SignIn.jsx';
import { ResetPassword } from './views/ResetPassword.jsx';
import { Welcome } from './views/Welcome.jsx';

const loggedInRoutes = {
'/account': Account,
'/note-details': NoteDetails,
'/': Notes,
};
const publicRoutes = {
'/sign-up': SignUp,
'/sign-in': SignIn,
'/reset-password': ResetPassword,
'/': Welcome,
};

export const Router = React.memo(() => {
const isLoggedIn = useCat(isLoggedInCat);

if (isLoggedIn) {
return ;
}

return ;
});
```

Use `enableAnimation` to control page transition animation (enabled by default).

```

```

### Navigate anywhere within your app:

```
import { navigateTo, replaceTo, goBack } from 'react-baby-router';

// This will add an item to the browser history
navigateTo('/note-details?noteId=123456');

...

// This will replace the current page, without adding an item to browser history
replaceTo('/account');

// go back
goBack();
```

### Navigate with a component:

```
import { Button } from '@radix-ui/themes';
import { BabyLink } from 'react-baby-router';
import React from 'react';

// Use your own link component
const RouteLink = React.memo(({ to, children }) => {
return {children};
});

function MyComponent() {
return (
<>
All my notes.
And this is my Account
>
)
}
```

That's everything.

See how I use **react-baby-router** in [easyy.click](https://github.com/penghuili/easyy.click) and [notenote.cc](https://github.com/penghuili/notenotecc)

You may noticed the `useCat` hook above, that's my simplest react state management lib: [usecat](https://github.com/penghuili/usecat)