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.
please add timesstamp
, video is great
font size so small,,, i am using googles, but the video is superb.
You should zoom in your screen
small font in -vs, not good
Thanks for sharing, the video is very helpful.
so almost like usePathname ?
source code pls
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?
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.
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
hello help me please
keep it up
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
Nice Job. This helped me.