0.7.8 • Published 4 years ago

react-router-url v0.7.8

Weekly downloads
30
License
MIT
Repository
github
Last release
4 years ago

React Router URL

A simple, flexible and zero dependencies router for React and Typescript support.

Update v0.7.6

It uses React hooks and URL api.

Table of Contents

Install

yarn add react-router-url

Get Started

Define your router

You can define your route names then use the hook useRouter to right your routes

type NestedRouteName = '/account' | '/account/settings' | '/account/contact';
const Account: React.FC = () => {
  return useRouter<RouteName>([
    { path: '/account/settings', component: <Settings /> },
    { path: '/account/contact', component: <Contact /> },
    { path: '/account', component: <AccountMenu /> },
  ]);
};

type RouteName = '/' | '/home' | '/account';

const Router: React.FC = () => {
  return useRouter<RouteName>([
    { path: '/', component: <Hello />, exact: true },
    { path: '/home', component: <Home /> },
    { path: '/account', component: <Account /> },
  ]);
};

After that you can use your Router component where you want.

About useRouter

You can use it to create Router componant every where. It's supported for nested router too.

Core

The core concept is base on history object and useURL hook.

history

It's based on window.history

push

This method will redirect you to the new url.

inputs :

  • nextUrl: the url target
  • params: if you need it you can add object with params

exemple :

history.push('/login')}
history.push('/account', { user: 'joe' })}

goBack

history.goBack();

canGoBack

if (history.canGoBack()) {
  history.goBack();
}

useURL

It's a hook for react component, watching path and params.

export const Account = () => {
  const { path, params } = useURL();
  return (
    <div>
      <div>Path : {path}</div>
      <div>User : {params.user}</div>
    </div>
  );
};

Components

Redirect

This component will redirect to his to props when he is rendering

<Redirect to="/" />
0.7.8

4 years ago

0.7.7

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.2

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.6.2

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.3.16

4 years ago

0.3.20

4 years ago

0.4.0

4 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.10

4 years ago

0.3.8

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.1.1

5 years ago