visit
1. Get an API key
Head over to the developer and create an account on a chrome browser. Your dashboard should look like this.
Click on the ‘Create an App’ button to create a new API. You will be prompted to select an option between API or SDK.
2. Fetch Data from Giphy API
Additionally, we’ll create states to hold the gif data and the term we search for.const [gifs, setGifs] = useState([]);
const [term, updateTerm] = useState('');
async function fetchGifs() {
try {
const API_KEY = <API_KEY>;
const BASE_URL = '//api.giphy.com/v1/gifs/search';
const resJson = await fetch(`${BASE_URL}?api_key=${API_KEY}&q=${term}`);
const res = await resJson.json();
setGifs(res.data);
} catch (error) {
console.warn(error);
}
}
3. Display the GIFs in the React Native UI
Let’s create an image list component to hold the GIFs in an image format. To achieve this, we wrote the code below:import React, {useState} from 'react';
import {View, TextInput, StyleSheet, FlatList, Image} from 'react-native';
// do not forget to add fresco animation to build.gradle
export default function App() {
const [gifs, setGifs] = useState([]);
const [term, updateTerm] = useState('');
async function fetchGifs() {
try {
const API_KEY = <API_KEY>;
const BASE_URL = '//api.giphy.com/v1/gifs/search';
const resJson = await fetch(`${BASE_URL}?api_key=${API_KEY}&q=${term}`);
const res = await resJson.json();
setGifs(res.data);
} catch (error) {
console.warn(error);
}
} /// add facebook fresco
function onEdit(newTerm) {
updateTerm(newTerm);
fetchGifs();
}
return (
<View style={styles.view}>
<TextInput
placeholder="Search Giphy"
placeholderTextColor='#fff'
style={styles.textInput}
onChangeText={(text) => onEdit(text)}
/>
<FlatList
data={gifs}
renderItem={({item}) => (
<Image
resizeMode='contain'
style={styles.image}
source={{uri: item.images.original.url}}
/>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
view: {
flex: 1,
alignItems: 'center',
padding: 10,
backgroundColor: 'darkblue'
},
textInput: {
width: '100%',
height: 50,
color: 'white'
},
image: {
width: 300,
height: 150,
borderWidth: 3,
marginBottom: 5
},
});
Important: For you to make GIFs appear on your Android device you have to add the following to the list of dependencies in your android/app/build.gradle.
implementation 'com.facebook.fresco:fresco:2.0.0'
implementation 'com.facebook.fresco:animated-gif:2.0.0'
4. Display advanced Giphy units such as stickers, trending GIFs and best matches
The video above show how the Giphy API React Native integration looks like with GIFs. You can easily search for Giphy stickers by replacing the BASE_URL
//api.giphy.com/v1/stickers/search
GIPHY Trending returns a list of the most relevant and engaging content each and every day. Our feed of trending content is continuously updated, so you always have the latest and greatest at your fingertips.And
GIPHY Translate converts words and phrases to the perfect GIF or Sticker using GIPHY’s special sauce algorithm. This feature is best exhibited in GIPHY’s Slack integration.
Conclusion
As we learned, adding GIFs support to any React Native app is extremely straightforward with the Giphy API integration in React Native. The generic REST endpoints provided by Giphy are extremely simple to call from React Native.