,

NextJS Sidebar Layout WITHOUT useState | NextJS 13/React | Tailwind CSS | useSelectedLayoutSegment

Posted by








NextJS Sidebar Layout

NextJS Sidebar Layout

Today, we will be creating a sidebar layout in NextJS without using useState. We will be using NextJS 13, React, Tailwind CSS, and the useSelectedLayoutSegment hook to achieve this.

Setting up NextJS

First, make sure you have NextJS installed. You can create a new NextJS project by running the following command in your terminal:

npx create-next-app@13 my-nextjs-app

This will create a new NextJS project with all the necessary files and dependencies.

Creating the Sidebar Layout

Next, we will create the sidebar layout using React and Tailwind CSS. We will also use the useSelectedLayoutSegment hook to manage the state of the sidebar.

Step 1: Install Tailwind CSS

Install Tailwind CSS in your NextJS project by running the following command:

npm install tailwindcss

Then, create a Tailwind CSS configuration file by running the following command:

npx tailwindcss init

Step 2: Create the Sidebar Component

Create a new file called Sidebar.js and add the following code:


import React from 'react';

const Sidebar = () => {
    return (
        

Sidebar

  • Option 1
  • Option 2
  • Option 3
); }; export default Sidebar;

Step 3: Create the useSelectedLayoutSegment Hook

Create a new file called useSelectedLayoutSegment.js and add the following code:


import { useState } from 'react';

const useSelectedLayoutSegment = () => {
    const [selectedSegment, setSelectedSegment] = useState(null);

    const selectSegment = (segment) => {
        setSelectedSegment(segment);
    };

    return {
        selectedSegment,
        selectSegment,
    };
};

export default useSelectedLayoutSegment;
    

Step 4: Implement the Sidebar in Your Layout

Finally, import the Sidebar component and the useSelectedLayoutSegment hook into your layout component and use them to create the sidebar layout.

With these steps, you have successfully created a sidebar layout in NextJS without using useState. You have used the useSelectedLayoutSegment hook to manage the state of the sidebar, and you have utilized React and Tailwind CSS to style the layout.


0 0 votes
Article Rating
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sandip parida
1 year ago

please add timesstamp
, video is great

Dulon Mahadi
1 year ago

font size so small,,, i am using googles, but the video is superb.

Fil2fip
1 year ago

You should zoom in your screen

Carlos Uriel Villegas
1 year ago

small font in -vs, not good

Wandesulu
1 year ago

Thanks for sharing, the video is very helpful.

Kami Gerami
1 year ago

so almost like usePathname ?

Prathamesh
1 year ago

source code pls

Rafavini
1 year ago

Hi, very good the video, but I have a problem, when I click on a URl it goes correctly, when I click on another it does not replace the one that was there it just concatenates what could be I'm using nextjs 13.4.13 would you know what could be happening?

Drew
1 year ago

Omg the typo of the tailwind class was driving me nuts the whole time, "gapy-y-5"… lol

Also how would the state be passed if the menu was generated from a json response of unknown values? That way the sidebar could be dynamically driven with pretty much any set of values following the given schema.

LucidiousXIV
1 year ago

Out of the many tutorials I've watched, this has honestly been the most helpful. I was able to follow along and listen better than most of the other videos I've come across

Hadja magamy Gbane
1 year ago

hello help me please

Basic's School
1 year ago

keep it up

Sudhir Belani
1 year ago

I have not seen your video, title was good but if you see the channel who has more than 1k subscriber they all share their codes. If Facebook has thought why should I provide all this free of cost then today nobody was knowing facebook. in short to grow sharing is necessary. all the best

Stephen Brown
1 year ago

Nice Job. This helped me.