GithubHelp home page GithubHelp logo

alphadevhq / react-nested-routes Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 350 KB

React nested routes is a react component which helps you to create nested routes in react application. It also provides a hook to get active path and active index of the route.

Home Page: https://www.npmjs.com/package/react-nested-routes

License: MIT License

JavaScript 38.89% TypeScript 61.11%
nested-routes react react-router-v6 react18 hook active-path

react-nested-routes's Introduction

Nested Routes React

This module will help you to implement the nested routes easily. it will give you a component NestedRoutes and a hook useActivePath where NestedRoutes will be used to wrap the required document which you want to show on the screen, and hook will be used to check which path is active actually and accordingly you can perform your operation.

This module will work only with the following versions:

    "react-router": "^6.0.2",
    "react": "^17.0.2"

Example

// App.js
import "./App.css";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import Home from "./components/Home";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/home/:tab" element={<Home />} />
        <Route path="/home" element={<Home />} />

        <Route path="*" element={<Navigate replace to="/home" />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;
// ./components/Home.jsx
import React from "react";
import Tab from "./Tab";
import { Link } from "react-router-dom";
import NestedRoute, { useActivePath } from "react-nested-routes";
import classNames from "classnames";

const Home = () => {
  const activePath = useActivePath({ parent: "home" });

  return (
    <div className="flex">
      <div className="flex flex-col p-2n w-1/2">
        <div className="text-xs py-1 text-gray-500 tracking-wider">
          Home/{activePath}
        </div>
        <div className="flex bg-gray-500 text-white">
          {["Profile", "Dashboard", "Statics"].map((item) => {
            return (
              <Link
                to={`/home/${item}`}
                className={classNames("block p-1 px-4", {
                  "bg-gray-400": activePath === item,
                })}
              >
                {item}
              </Link>
            );
          })}
        </div>
        {["Profile", "Dashboard", "Statics"].map((item) => {
          return (
            <NestedRoute path={item} parent="home">
              <Tab tab={item} />
            </NestedRoute>
          );
        })}{" "}
      </div>
    </div>
  );
};

export default Home;
// ./components/Tab.jsx
import React from "react";

const Tab = (props) => {
  const { tab } = props;
  return (
    <div className="flex flex-col items-center p-10 py-32 justify-between w-full font-bold bg-gray-200">
      <div className="text-gray-700 text-3xl">{tab}</div>
    </div>
  );
};

export default Tab;

Output

"Example"

react-nested-routes's People

Contributors

abdheshnayak avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.