Welcome to my blog on creating a custom GIF search feature in React. In this blog post, I'll be using the Giphy API to access a vast collection of GIFs and integrate them into a React application.
Before we begin, you'll need to sign up for a Giphy API key. This key will be used to authenticate your requests to the Giphy API. Once you have your key, you're ready to start building your GIF search feature.
We'll be using React as our front-end framework and Tailwind CSS for styling. If you're not familiar with these technologies, don't worry - I'll provide all the necessary code and explain everything as we go along.
Setup
Let's start by setting up a new React project and installing the necessary dependencies. In your terminal, run the following commands:(you can use whatever you want or CodesandBox)
javascript1npx create-tw@latest 2cd appname 3yarn dev
With our project set up, we can now start building out our GIF search feature. We'll begin by creating a simple search bar that allows the user to input their search query. Next, we'll use the Giphy API to retrieve a list of GIFs that match the user's query. Finally, we'll display these GIFs in our application.
Throughout the tutorial, we'll be breaking down each step of the process, so you can follow along and build your GIF search feature.
So, let's begin!
Let's create a simple search bar
javascript1//App.jsx 2<div> 3 <input type="text" placeholder="LOL" /> 4 <button type="button">Search</button> 5</div>
Add Some Style to the search bar
javascript1//App.jsx 2<div className="flex items-center border-b border-teal-500 py-2"> 3 <input 4 className="appearance-none bg-transparent border-none w-full text-gray-700 mr-3 py-1 px-2 leading-tight focus:outline-none" 5 type="text" 6 placeholder="LOL" 7 /> 8 <button 9 className="flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded" 10 type="button" 11 > 12 Search 13 </button> 14 </div>
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674533460896/b4f34ad9-cb77-4450-9c46-dfbf137b80db.png align="center")
Create a State for fetching input data entered by the user, a state for storing API data, and a method when the user clicks on the search button
javascript1//App.jsx 2const [input, setInput] = useState(""); 3const [data, setData] = useState([]); 4 5 const handleSearch = () => { 6 console.log(input); 7 };
The initial value of the state variable is set to an empty string "" . This state variable will be used to store the value of the search input so that we can use it to make a request to the Giphy API later on.
Now let's add onChnage and onClick on button
javascript1<div className="flex items-center border-b border-teal-500 py-2"> 2 <input 3 className="appearance-none bg-transparent border-none w-full text-gray-700 mr-3 py-1 px-2 leading-tight focus:outline-none" 4 type="text" 5 placeholder="LOL" 6 aria-label="gif" 7 value={input} 8 onChange={(e) => setInput(e.target.value)} 9 /> 10 <button 11 className="flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded" 12 type="button" 13 onClick={handleSearch} 14 > 15 Search 16 </button> 17 </div>
The input element has a value
attribute that is set to the current value of the input
state variable we defined earlier. It also has an onChange
event handler that is set to a callback function which takes the event object as an argument, it then calls the setInput
function that updates the state with the current value of the input field.
An onClick
event handler that is set to a callback function named handleSearch
which will be used to make the request to the Giphy API with the current value of the input
state variable when the button is clicked.
Let's add API fetching logic in our handleSearch
javascript1 const handleSearch = () => { 2 fetch( 3 `https://api.giphy.com/v1/gifs/search?api_key=YOUR_KEY&q=${input}&limit=25&offset=0&rating=g&lang=en` 4 ) 5 .then((res) => res.json()) 6 .then((data) => setData(data.data)) 7 .catch((err) => console.log(err)); 8 };
The function uses the fetch
method to make a GET request to the Giphy API with the search query as part of the URL. The API key is also included in the URL, so the API can authenticate the request. The q
parameter in the URL is used to specify the search query, and it's set to the current value of the input
state variable. The limit
parameter is used to specify the number of results that should be returned, offset
parameter to specify the starting point of the result set, rating
for filter the search by rating and lang
for language of the results.
Let's render API data on UI
After the Search button div add this.
javascript1<div className="grid grid-cols-3 gap-4"> 2 {data?.map((el) => ( 3 <div 4 key={el.id} 5 className="max-w-sm rounded overflow-hidden shadow-lg" 6 > 7 <img 8 className="w-full" 9 src={el.images.original.url} 10 alt="Sunset in the mountains" 11 /> 12 </div> 13 ))} 14 </div>
It is using a JavaScript map method to iterate through the data returned from the API, which is stored in the data
state variable. The map method is called on the data
array and for each element in the array, it returns a new div
element.
This code is responsible for displaying the GIFs returned from the API in a grid layout on the screen. The key
attribute is used for performance optimization in React, it helps React to identify which items have changed, been added, or been removed.
Putting it all together: The Complete Code for Building a Dynamic GIF Search
javascript1import React, { useState } from "react"; 2import "./app.css"; 3 4const App = () => { 5 const [input, setInput] = useState(""); 6 const [data, setData] = useState([]); 7 8 const handleSearch = () => { 9 fetch( 10 `https://api.giphy.com/v1/gifs/search?api_key=YOUR_KEY&q=${input}&limit=25&offset=0&rating=g&lang=en` 11 ) 12 .then((res) => res.json()) 13 .then((data) => setData(data.data)) 14 .catch((err) => console.log(err)); 15 }; 16 17 return ( 18 <> 19 <div className="flex justify-center items-center p-8"> 20 <div className="flex items-center border-b border-teal-500 py-2"> 21 <input 22 className="appearance-none bg-transparent border-none w-full text-gray-700 mr-3 py-1 px-2 leading-tight focus:outline-none" 23 type="text" 24 placeholder="LOL" 25 aria-label="gif" 26 value={input} 27 onChange={(e) => setInput(e.target.value)} 28 /> 29 <button 30 className="flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded" 31 type="button" 32 onClick={handleSearch} 33 > 34 Search 35 </button> 36 </div> 37 </div> 38 39 <div className="grid grid-cols-3 gap-4"> 40 {data?.map((el) => ( 41 <div 42 key={el.id} 43 className="max-w-sm rounded overflow-hidden shadow-lg" 44 > 45 <img 46 className="w-full" 47 src={el.images.original.url} 48 alt="Sunset in the mountains" 49 /> 50 </div> 51 ))} 52 </div> 53 </> 54 ); 55}; 56 57export default App;
Ready to Try? Play with the Live Version of the GIF Search Built in React
I hope that you found this blog helpful and were able to follow along to build your own custom GIF search feature. I also hope that you have learned a lot about how to use the Giphy API, React hooks, and Tailwind CSS to create a dynamic and interactive user interface.
Thank you for checking out this blog. If you have any further questions or want more information on this topic, please let me know. I would be happy to help!
In addition, Thanks for your time and patience in reading this blog. If you have any suggestions or feedback, I would be glad to hear from you. I hope you enjoyed building this feature and I would be excited to see what you will come up with next.