Next.js toggle between grid and list view

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks Tapas!
Glad you like them, also planning on doing more of a React background set now
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we'll be building a simple yet super effective toggle function. It will give us the option to toggle between the list and grid view.
The result will work like the video below.

If you want to follow along, please use the following branch as your starting point.
Since Next.js is React, we can use all the fantastic React state management tools, such as the useState, which we'll use for this purpose.
Open up your pages/index.js file and import the useState from React.
import {useState} from 'react';
Then inside our page, we can define the state and its default value. In our case, we'll use a boolean, where the default value (false) means it's in grid view, and if it's true, it will be in list view.
const [toggleViewMode, setToggleViewMode] = useState(false);
The toggleViewMode will become the variable that we can read and use, and at the end of the line, you see false, which sets its default value.
The setToggleViewMode is the function we can call to change the value of this variable.
Let's go ahead and add a button that, on click, can change our variable.
<div className="flex justify-end p-5">
<button
className="px-4 py-2 font-bold text-white bg-blue-500 rounded-full hover:bg-blue-700"
onClick={() => setToggleViewMode(!toggleViewMode)}
>
{toggleViewMode ? 'grid' : 'list'}
</button>
</div>
Add this code above the current wrapping div
The main things to watch for here are the onClick function, which will invoke every time we click this button. This button then calls the setToggleViewMode and passes the negative value it currently has.
Remember using
!in JavaScript is the logical "not" operator.
And the next part is based on what the current value is. So if the value is true, we show the grid. Else we show the list.
Now that we have this state and button working, we need to change our main wrapping div.
Currently it looks like this:
<div className='grid grid-cols-3 gap-4 p-5'>
Note the grid-cols-3 as this states the content should be split into three columns.
We want to show three columns if we are in grid mode. Else only one column, which will resemble list mode.
Change your class to look like this:
<div className={`grid grid-cols-${toggleViewMode ? '1' : '3'} gap-4 p-5`}>
This will use the number we need based on the view mode variable. And voila, we now have a grid/list mode toggle in Next.js.
You can find this complete code example on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter