How to Add Image Grid Gallery to React Apps
The react-image-grid-gallery
is a simple and easy-to-use component with a lightbox for displaying a grid of images in React apps.
Here are some of the package’s features:
- SEO friendly
- Fullscreen support
- Keyboard accessible
- Mobile responsive
- Lightbox with translucent background
- Set column numbers dynamically or manually
This article will use a simple project to explain how to use the library to add image galleries to your React applications.
Here is a demo of what we will build:
So, without further ado, let’s start with the first step.
Step 1: Get the Right Node and NPM Version
Make sure you have Node 10.16 (or greater) and NPM 5.6 (or greater) installed on your system.
Step 2: Create a New React App
Use the create-next-app
package to create a new React app.
Alternatively, you can use Yarn or pnpm to configure your project like so:
After running the above command, Next will ask the following questions:
- Ok to proceed? (y) Enter the y key on your keyboard to proceed.
- What is your project named? You can name it anything you wish—for instance,
react-image-grid-gallery-demo-001
. - Would you like to use TypeScript with this project? » No / Yes Select No, as we will use plain JavaScript in this tutorial.
- Would you like to use ESLint with this project? » No / Yes Select No as we do not wish to use ESLint in this tutorial.
- Would you like to use Tailwind CSS with this project? » No / Yes Select No as we do not wish to use Tailwind CSS in this tutorial.
- Would you like to use
src/
directory with this project? » No / Yes Select No as we do not wish to use thesrc/
directory in this tutorial. - Use App Router (recommended)? » No / Yes Select Yes as we are ok using Next’s App router configurations. (Don’t worry if you know nothing about the App Router. We are only using Next’s basic functionalities to learn how to create an image gallery.)
- Would you like to customize the default import alias? » No / Yes Select No as we are okay with using Next’s default import alias for this test-app.
Once you’ve answered all the questions, Next.js will create your new app.
Step 3: Go Inside the Project Directory
After the installation process, navigate into the project directory like so:
Step 4: Install the Image Grid Gallery Package
Using your text editor, install the React Image Grid Gallery locally.
Step 5: Import the Image Grid Gallery Package
Open the page.js
file located in the app folder of your Next.js application.
Then, delete all its content. And import the image grid gallery package like so:
Step 6: Render the Image Gallery Component
After importing the gallery package, render it to the DOM as follows:
The ImageGallery
component accepts four props:
Props | Type | Default | Description |
---|---|---|---|
imagesInfoArray | array | undefined | (Required) An array of objects containing the following properties:
|
columnCount | number or keyword (string) | "auto" | (Optional) The number of columns. |
columnWidth | number or keyword (string) | 230 | (Optional) The minimum width of the gallery’s columns. |
gapSize | number | 24 | (Optional) The gallery’s gap size. |
Express Your Love for Coding!
Step 7: Run the Application
Take a look at your app in the browser by running the following:
Note for Docusaurus users
Did you get a ReferenceError: crypto is not defined
error during the build step? If so, this note is for you.
Wrap the ImageGallery
component in <BrowserOnly>
if you get a ReferenceError: crypto is not defined
error during your build step.
Example:
The <BrowserOnly>
component tells Docusaurus to render the ImageGallery
library only in the browser.
Note for Remix Users
Remix users should add "react-image-grid-gallery"
to their remix.config.js
file:
The serverDependenciesToBundle
field tells Remix to transpile and include the "react-image-grid-gallery"
package in the server bundle.
Live Demo
Below is a live demo of the React Image Grid Gallery.
Although we used the react-image-grid-gallery
library to create an image gallery in a simple project, you can use the same principles on any React application.