react-mini-router-hook

1.0.2 • Public • Published

react-mini-router-hook

Mini Router for React based on Hooks and Context. 1.7k

Declare your Routes

import { Router, Route } from "react-mini-router-hook";

export default function App() {
  return (
  <Router location={window.location.pathname}>
    <Route path="/" component={Home} />
    <Route path="/users/:id" component={Users} />
  </Router>);
}

Change route from a component

import { RouterContext } from 'react-mini-router-hook';

export default function NavBar() {
  const { setPath } = React.useContext(RouterContext);

  return (
    <ul>
      <li onClick={() => setPath('/users/1')}>Menu Item 1</li>
      <li onClick={() => setPath('/users/2')}>Menu Item 1</li>
  <ul>);
}

Get current path

import { RouterContext } from 'react-mini-router-hook';

export default function Helmet() {
  const { path } = React.useContext(RouterContext);

  return (<title>{path}</title>);
}

Readme

Keywords

Package Sidebar

Install

npm i react-mini-router-hook

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

59.9 kB

Total Files

10

Last publish

Collaborators

  • avrmav