react-ctx-router
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

react-ctx-router

Simple React router for simple SPAs. Uses React Context and Hooks for maximum simplicity

yarn add react-ctx-router

// setup router
import { Provider } from "react-ctx-router";
 
const routes = [
  { identifier: "main", route: "/" },
  { identifier: "about", route: "/about" },
  { identifier: "item", route: "/items/:id" }
];
 
const WrappedApp = (
  <Provider routes={routes}>
    <App />
  </Provider>
);
// access current route
import { useRoute } from "react-ctx-router";
 
const Header = () => {
  const [id, params] = useRoute();
  if (id === "item") {
    return <b>You are viewing item {params.id}</b>;
  } else {
    return <i>Please select an item</i>
  }
};
// modify route
import { useRouteMutator } from "react-ctx-router";
 
const Page = () => {
  const mutateRoute = useRouteMutator();
  return (
    <>
      <div onClick={() => mutateRoute("item", {id: "1"})}>View item "1"</div>
      <div onClick={() => mutateRoute("item", {id: "2"})}>View item "2"</div>
      <div onClick={() => mutateRoute("about")}>About</div>
    </>
  );
};

Readme

Keywords

none

Package Sidebar

Install

npm i react-ctx-router

Weekly Downloads

2

Version

0.0.6

License

MIT

Unpacked Size

12.4 kB

Total Files

14

Last publish

Collaborators

  • wyozi