Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page-wrap usage #508

Open
tekknow opened this issue Jul 12, 2024 · 2 comments
Open

page-wrap usage #508

tekknow opened this issue Jul 12, 2024 · 2 comments

Comments

@tekknow
Copy link

tekknow commented Jul 12, 2024

I'm a confused react newbie. How do you use the menu to actually go to a specific clicked item page, not the main page that is in the demo? Currently, it looks like example.js contains class Demo. The only argument passed to its constructor is props. Its render() function returns the html for the <main id="page-wrap">. But it's always the same page. How to go to a different page depending on what is clicked in the menu?

Typically react uses a <Router> to do that, as in import { BrowserRouter as Router, Route, Link, Routes } from "react-router-dom";
I can't figure out how to reconcile these two strategies. Can you help?

@gitlep1
Copy link

gitlep1 commented Jul 30, 2024

If you're still looking for a solution, here's an approach that might work for you. You can use the "useNavigate" hook from "react-router-dom" to navigate users to different routes when they click a menu item in your sidebar. Here's a step-by-step example:

File 1 (Main Component with Sidebar):

import { useState } from "react";
import { pushRotate as SidebarMenu } from "react-burger-menu";
import File2 from "./File2";

const File1 = () => {
  const [isOpen, setIsOpen] = useState(false);

  const handleSidebarOpen = () => {
    setIsOpen((prevOpen) => !prevOpen);
  };

  return (
    <div id="outer-container">
      <SidebarMenu
        outerContainerId={"outer-container"}
        pageWrapId={"page-wrap"}
        isOpen={isOpen}
        onStateChange={({ isOpen }) => setIsOpen(isOpen)}
      >
        <File2 handleSidebarOpen={handleSidebarOpen} />
      </SidebarMenu>
      <main id="page-wrap">
        {/* Your main page content here */}
      </main>
    </div>
  );
};

export default File1;

File 2 (Sidebar Menu Component):

import { useNavigate } from "react-router-dom";

const File2 = ({ handleSidebarOpen }) => {
  const navigate = useNavigate();

  const handleNavigation = (path) => {
    navigate(path);
    handleSidebarOpen(); // Optionally close the sidebar after navigation
  };

  return (
    <section>
      <div onClick={() => handleNavigation("/")}>Home</div>
      <div onClick={() => handleNavigation("/about")}>About</div>
      <div onClick={() => handleNavigation("/rankings")}>Rankings</div>
      {/* Add more menu items as needed */}
    </section>
  );
};

export default File2;

Here's an explanation of what's happening:

  1. File 1 (File1.js):
    This component uses the react-burger-menu library to create a sidebar menu.
    The SidebarMenu component is configured with outerContainerId and pageWrapId to define which elements should move with the sidebar animation.
    The isOpen state controls whether the sidebar is open or closed.
    The handleSidebarOpen function toggles the sidebar's state.

  2. File 2 (File2.js):
    This component renders the actual menu items. It uses the useNavigate hook from react-router-dom to programmatically navigate to different routes when a menu item is clicked.
    The handleNavigation function is called with the desired route path, triggering the navigation and optionally closing the sidebar.

In this setup, when a menu item is clicked, the navigate function updates the URL, which in turn renders the corresponding component for that route. This is a common pattern in React applications using react-router-dom for navigation. It's how I manage navigation in my own projects when I use react-burger-menu.

@tekknow
Copy link
Author

tekknow commented Jul 30, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants