How To Add Images From API To Tailwind Card in React?
Last Updated :
16 Sep, 2024
Displaying images fetched from an API in a Tailwind-styled card in React involves data fetching, state management, and styling. In this article, we will see how we can add images from an API to a Tailwind Card in React.
Approach
The approach involves the following steps:
- Setup React Project: Create a React app using Vite and set up Tailwind CSS for styling.
- Create a Card Component: Build a reusable card component using Tailwind CSS.
- Fetch Data from API: Use Axios or Fetch API to get image data from a source like Unsplash.
- Display Images: Map over the fetched data and render the images inside the Tailwind card component.
This structured approach ensures the app is modular, easy to maintain, and scalable for adding more features later.
Steps To Add Images from API to Tailwind Card in React
Step 1: Set Up a React Application
Create a new React project using Vite:
npx create-vite@latest my-react-app --template react
cd my-react-app
npm install
Step 2: Install Tailwind CSS
Install Tailwind CSS and set it up:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Configure Tailwind in tailwind.config.js:
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
Add Tailwind’s CSS to src/index.css:
/* src/index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
Folder Structure
Folder StructureDependencies
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.20",
"eslint": "^9.9.0",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.9",
"globals": "^15.9.0",
"postcss": "^8.4.45",
"tailwindcss": "^3.4.11",
"vite": "^5.4.1"
}
Step 3: Create the Card Component
Create a card component to display the images.
JavaScript
// src/Card.js
const Card = ({ image, title }) => {
return (
<div className="max-w-sm rounded overflow-hidden shadow-lg bg-white">
<img className="w-full" src={image} alt={title} />
<div className="px-6 py-4">
<div className="font-bold text-xl mb-2">{title}</div>
</div>
</div>
);
};
export default Card;
Step 4: Fetch Images from an API
Fetch images from an API and display them using the ImageCard component.
JavaScript
// src/App.js
import React, { useEffect, useState } from "react";
import Card from "./Card";
const App = () => {
const [images, setImages] = useState([]);
useEffect(() => {
const fetchImages = async () => {
try {
const response = await fetch(
"https://api.unsplash.com/photos/?client_id=Your API Key"
);
const data = await response.json();
setImages(data);
} catch (error) {
console.error("Error fetching images:", error);
}
};
fetchImages();
}, []);
return (
<div className="container mx-auto p-4 grid grid-cols-1
sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
{images.map((image) => (
<Card
key={image.id}
image={image.urls.small}
title={image.alt_description || "Untitled"}
/>
))}
</div>
);
};
export default App;
To start the application run the following command.
npm run dev
Output
Add Images from an API to a Tailwind Card in React
Similar Reads
How to Add Background Image Overlay in Tailwind CSS?
Adding a background overlay to your Tailwind CSS project can help you create visually appealing layouts that layer content over the background image. In this article, we'll demonstrate how to add a background overlay using the Tailwind CSS utility class.ApproachWe will use Tailwind CSS to create a s
2 min read
How to Install Tailwind CSS with Create React App?
We will see how to set up Tailwind CSS in a React project using the Create React App. Tailwind CSS is a utility-first CSS framework that allows for rapid development of custom user interfaces. When combined with Create React App, it offers a flexible approach to build modern React applications.Prere
2 min read
How to setup Tailwind 3 in React with CRA 5 ?
In this article, we will see how to set up Tailwind 3 in React with CRA 5, but before that, we need some basic ideas about these technologies. React JS: A free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook.TailwindCSS: A high
3 min read
How to Add a Image to React Bootstrap dropdown ?
In this article, we will see how to add an image to the React Bootstrap dropdown. It is a React-based implementation of the Bootstrap components. Dropdown Component provides a way to display lists of links or more actions within a menu when clicked over it. Steps to Create React Application and Inst
3 min read
How to Create Tailwind CSS Profile Card in React?
TailwindCSS can be integrated with React to create highly customizable and responsive profile cards by using utility-first classes for styling. This approach allows you to design visually appealing cards with gradients, rounded corners, and interactive animations, all while ensuring seamless respons
3 min read
How to Set Background Images in ReactJS
Setting the background images in React improves the UI and user experience of web apps by making the appearance better. These images can be some shape or shade using color gradients. In this tutorial, we will look at different methods to apply background images to your react application. How to Set
4 min read
How to crop images in ReactJS ?
Image cropping is a common requirement in web applications, especially when dealing with user-generated content, photo editing, or image manipulation. ReactJS, being a popular JavaScript library for building user interfaces, provides various techniques and libraries that can be used to implement ima
3 min read
How to add Image Carousel in Next.js ?
In this article, we are going to learn how we can add an Image Carousel in NextJS. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac.ApproachTo create image carousel in next.js we are going to use a react-re
2 min read
How to use Tailwind CSS with Next.js Image ?
Integrating Tailwind CSS with Next.js enables you to style your application efficiently using utility-first CSS. When combined with the Next.js Image component, you can create responsive and optimized images with ease and consistency. In this article, we will learn how to use Tailwind CSS with Next.
3 min read
How to add scale animation on hover using Tailwind CSS in React ?
In this article, we'll see how to add scale animation on hover using tailwind CSS in a ReactJS app. The hover effect appears when a user positions the cursor over an element. In tailwind CSS, the scale utility class helps in getting the zoom effect over an element. PrerequisitesReact JSTailwind CSSA
3 min read