react-use-path

The tiniest react router implemented with hooks.

Usage no npm install needed!

<script type="module">
  import reactUsePath from 'https://cdn.skypack.dev/react-use-path';
</script>

README

react-use-path

NPM version Build Status Test Coverage Dependency Status DevDependency Status License PR Welcome

The tiniest react router implemented with hooks.

Introduction

The React brand new feature hooks changes the way how we write and organize components. Writing an app fully with functional components becomes possible. The first thing I'd like to try is to replace the heavy react-router, and create a simple yet powerful functional router.

Finally the API really stunned me.

const [path, setPath] = usePath();

It can't be more simple yet powerful. It makes the coding style of an app looks more pure functional and consistent.

Installation

Install this package with npm.

npm i react-use-path -s

Usage

Use javaScript language level switch.

import React from 'react';
import usePath from 'react-use-path';

const App = () = {
  const [path, setPath] = usePath();
  switch (path.path) {
    case '/':
      return <HomePage />;
    case '/products':
      return <ProductListPage />;
    case '/profile':
      return <UserPage />;
    default:
      return <PageNotFound />;
  }
}

Use your own switch. You should find a package that implements these components or implement your own.

import React from 'react';
import usePath from 'react-use-path';

const App = () = {
  const [path, setPath] = usePath();
  return <Switch value={path.path}>
    <Case value='/'>
      <HomePage />
    </Case>
    <Case value='/products'>
      <ProductsPage />
    </Case>
    <Case value='/profile'>
      <UserPage />
    </Case>
    </Default>
      <PageNotFound />
    </Default>
  </Switch>;
}

Use your own router component. You should find a package that implements these components or write your own.

import React from 'react';
import usePath from 'react-use-path';

const App = () = {
  const [path, setPath] = usePath();
  return <Router location={path.fullpath} setLocation={setPath}>
    <Route match='/'>
      <HomePage />
    </Route>
    <Route match='/products'>
      <ProductsPage />
    </Route>
    <Route match='/products/:id'>
      <ProductPage />
    </Route>
    <Route match='/profile'>
      <UserPage />
    </Route>
    </Route match='*'>
      <PageNotFound />
    </Route>
  </Router>;
}

Redirection is just a line of code.

import React from 'react';
import usePath from 'react-use-path';

const App = () = {
  const currentUser = useUser();
  const [path, setPath] = usePath();
  if (!currentUser) {
    return setPath('/sign-in');
  }
  return <div>...</div>;
}

API

path

path is an object which contains 4 values. Let's say, the current url is http://www.example.com/part1/part2?val1=2&val2=5#top. The path value is:

{
  "path": "/part1/part2;",
  "query": "val1=2&val2=5",
  "hash": "top",
  "fullpath": "/part1/part2?val1=2&val2=5#top"
}

setPath

  • setPath(path: string)

Set new path value with a full path string.

setPath('/foo/bar?baz=true'); // => '/foo/bar?baz=true'
  • setPath(path: object)

Set new path component with an object.

setPath({ hash: 'bottom' }) // => '/foo/bar?baz=true#bottom'
setPath({ query: 'baz=false' }) // => '/foo/bar?baz=false'
setPath({ path: '/another/path' }) // => '/another/path'
setPath({
  path: '/user',
  query: 'hidePassword=true'
}) // => '/user?hidePassword=true'

License

MIT © Zhang Kai Yu