Create a meme generator by using ReactJS Last Updated : 15 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In this tutorial, we’ll create a meme generator using ReactJS. In the meme generator, we have two text fields in which we enter the first text and last text. After writing the text when we click the Gen button, it creates a meme with an image and the text written on it. Preview Image: meme generator using react PrerequisiteThe pre-requisites for this project are: Functional and Class ComponentsReact AJAX and APIApproach to Create Meme Generator using React JSTo create the meme generator we will be using the api 'https://api.imgflip.com/get_memes' and fetch method to get and render the meme. It will also have 2 text inputs to add the text to the rendered image along with a button to update a new meme on the page. Steps to Create Meme Generator using ReactStep 1: Create new React app and install the required modules. Check this article for setting up the react app. Step 2: Define the memeGenerator component and coresponding stylings as given in below example. Example: This example demonstrates the meme-generator app using the api to generate and display random memes. CSS /*Filename - App.css*/ .meme { position: relative; width: 59%; max-width: 500px; max-height: 500px; margin: auto; text-align: center; } .meme > img { margin: auto; height: 50%; max-width: 500px; max-height: 500px; } .meme > h2 { position: absolute; width: 80%; text-align: center; left: 50%; transform: translateX(-50%); margin: 15px 0; padding: 0 5px; font-family: impact, sans-serif; font-size: 1em; text-transform: uppercase; color: white; letter-spacing: 1px; text-shadow: 2px 2px 0 #000; } .meme > .bottom { bottom: 0; } .meme > .top { top: 0; } form { padding-top: 25px; text-align: center; } JavaScript // Filename - App.js import React from 'react' import './App.css' class App extends React.Component { state = { topText: '', bottomText: '', allMemeImgs: [], randomImg: '' } // componentDidMount() method to fetch // images from the API componentDidMount () { // Fetching data from the API fetch('https://api.imgflip.com/get_memes') // Converting the promise received into JSON .then(response => response.json()) .then(content => // Updating state variables this.setState({ allMemeImgs: content.data.memes }) ) } // Method to change the value of input fields handleChange = event => { // Destructuring the event. target object const { name, value } = event.target // Updating the state variable this.setState({ [name]: value }) } // Method to submit from and create meme handleSubmit = event => { event.preventDefault() const { allMemeImgs } = this.state const rand = allMemeImgs[Math. floor(Math.random() * allMemeImgs.length)].url this.setState({ randomImg: rand }) } render () { return ( <div> {/* // Controlled form */} <form className='meme-form' onSubmit={this.handleSubmit}> {/* // Input field to get First text */} <input placeholder='Enter Text' type='text' value={this.state.topText} name='topText' onChange={this.handleChange} /> {/* // Input field to get Lsst text */} <input placeholder='Enter Text' type='text' value={this.state.bottomText} name='bottomText' onChange={this.handleChange} /> {/* // Button to generate meme */} <button>Generate</button> </form> <br /> <div className='meme'> {/* // Only show the below elements when the image is ready to be displayed */} {this.state.randomImg === '' ? ( '' ) : ( <img src={this.state.randomImg} alt='meme' /> )} {this.state.randomImg === '' ? ( '' ) : ( <h2 className='top'>{this.state.topText}</h2> )} {this.state.randomImg === '' ? ( '' ) : ( <h2 className='bottom'>{this.state.bottomText}</h2> )} </div> </div> ) } } export default App Step 3: Run the app using this command in the terminal: npm startOutput: This output will be visible on the http://localhost:3000 on browser window Comment More infoAdvertise with us P pushpendrayadav1057 Follow Improve Article Tags : Project Web Technologies ReactJS Web Development Projects ReactJS-Projects +1 More Similar Reads BMI Calculator Using React In this article, we will create a BMI Calculator application using the ReactJS framework. A BMI calculator determines the relationship between a person's height and weight. It provides a numerical value that categorizes the individual as underweight, normal weight, overweight, or obese.Output Previe 3 min read Create Rock Paper Scissor Game using ReactJS In this article, we will create Rock, Paper, Scissors game using ReactJS. This project basically implements class components and manages the state accordingly. The player uses a particular option from Rock, Paper, or Scissors and then Computer chooses an option randomly. The logic of scoring and win 6 min read Create a Form using React JS Creating a From in React includes the use of JSX elements to build interactive interfaces for user inputs. We will be using HTML elements to create different input fields and functional component with useState to manage states and handle inputs. Prerequisites:Functional ComponentsJavaScript ES6JSXPr 5 min read Create a Random Joke using React app through API In this tutorial, we'll make a website that fetches data (joke) from an external API and displays it on the screen. We'll be using React completely to base this website. Each time we reload the page and click the button, a new joke fetched and rendered on the screen by React. As we are using React f 3 min read Nutrition Meter - Calories Tracker App using React GeeksforGeeks Nutrition Meter application allows users to input the name of a food item or dish they have consumed, along with details on proteins, calories, fat, carbs, etc. Users can then keep track of their calorie intake and receive a warning message if their calorie limit is exceeded. The logic 9 min read Currency converter app using ReactJS In this article, we will be building a very simple currency converter app with the help of an API. Our app contains three sections, one for taking the user input and storing it inside a state variable, a menu where users can change the units of conversion, and finally, a display section where we dis 4 min read Lap Memory Stopwatch using React Stopwatch is an application which helps to track time in hours, minutes, seconds, and milliseconds. This application implements all the basic operations of a stopwatch such as start, pause and reset button. It has an additional feature using which we can keep a record of laps which is useful when we 5 min read Typing Speed Tester using React In this article, we will create a Typing Speed Tester that provides a random paragraph for the user to type as accurately and quickly as possible within a fixed time limit of one minute. This application also displays the time remaining, counts mistakes calculates the words per minute and characters 9 min read Number Format Converter using React In this article, we will create Number Format Converter, that provides various features for users like to conversion between decimal, binary, octal and hexadecimal representations. Using functional components and state management, this program enables users to input a number and perform a range of c 7 min read Create a Password Validator using ReactJS Password must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in ReactJS. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions a 2 min read Like