Skip to content

Instantly share code, notes, and snippets.

@kinxori
Last active May 14, 2023 08:33
Show Gist options
  • Save kinxori/da757f6ffefb1dc5faaa78a1fb1d70c2 to your computer and use it in GitHub Desktop.
Save kinxori/da757f6ffefb1dc5faaa78a1fb1d70c2 to your computer and use it in GitHub Desktop.
Create Random Emoji Generator function. We are using React and fetching from "emoji-api.com". (updates every time your father component has a movement, not fixed yet 🥲)
import { useEffect, useState } from "react"; // Import your React hooks
const [emojiData, setEmojiData] = useState([]); // State variable to store the emoji data
const EmojiAPI = "https://emoji-api.com/emojis?access_key=👺Register to the page and generate your unique key👺"
//👺Atention here
useEffect(() => { // We use useEffect hook to fetch our API everytime our page loads
const fetchEmojiData = async () => {
const response = await fetch(EmojiAPI)
const emojiFetchedData = await response.json()
setEmojiData(emojiFetchedData)
};
fetchEmojiData() //callback your fetchEmojiData fetch !! important !!
}, []);
const randomEmojiIndex = Math.floor(Math.random()* emojiData.length); //generate a random number based on our data length
const randomEmoji = emojiData[randomEmojiIndex] //Console.log the randomEmoji.codePoint,
//you should get something like this "U+1F929"
const generatedEmoji = `${String.fromCodePoint(parseInt(randomEmoji.codePoint, 16))}` // Convert codePoint to emoji!! 😵
// depending on your emoji you should console.log( generatedEmoji ) and get an emoji: 🥸
// that's all!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment