How To Create a Responsive Like Button in ReactJS?
Last Updated :
01 Oct, 2024
The responsive like button is an important feature in many web applications, enabling user engagement with content. When implementing this feature, making it both functional and responsive ensures a smooth user experience. In this article, we'll explore how to create a responsive like button in ReactJS, to ensure the button works well across devices and screen sizes.
Steps to Create a Responsive Like Button in ReactJS
Step 1: Set Up React Application
Before you create the like button, ensure that you have a React application set up. If not, you can use the following command to create one:
npx create-react-app react-gfg
cd react-gfg
Folder Structure
Folder StructureDependencies
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Step 2: Create the Like Button Component
Create a new component, LikeButton.js. This component will handle the button’s state and responsiveness.
JavaScript
//LikeButton.js
import React, { useState } from 'react';
const LikeButton = () => {
// State to keep track of the like status
const [liked, setLiked] = useState(false);
// Toggle like status
const toggleLike = () => {
setLiked(!liked);
};
return (
<button
onClick={toggleLike}
className={`like-button ${liked ? 'liked' : ''}`}
>
{liked ? '❤️ Liked' : '♡ Like'}
</button>
);
};
export default LikeButton;
Step 3: Add Responsive Styling
Next, you need to add responsive styling for the like button. You can do this using CSS. Create a LikeButton.css file and add the following styles:
CSS
/* LikeButton.css */
.like-button {
padding: 10px 20px;
font-size: 18px;
border: none;
background-color: white;
color: #333;
cursor: pointer;
transition: background-color 0.3s ease;
}
.like-button:hover {
background-color: #f0f0f0;
}
.like-button.liked {
color: red;
}
@media (max-width: 768px) {
.like-button {
font-size: 16px;
padding: 8px 15px;
}
}
@media (max-width: 480px) {
.like-button {
font-size: 14px;
padding: 6px 12px;
}
}
Step 4: Integrate the component is your App.js file
JavaScript
//App.js
import React from 'react'
import LikeButton from './LikeButton'
const App = () => {
return <LikeButton />
}
export default App
To start the application run the following command.
npm start
Output
Create a Responsive Like Button in ReactJS
Similar Reads
How to create Instagram Like button in ReactJS? We can create Instagram Like Button in ReactJS using the checkbox component, FormControlLabel component, and Icon component. Material UI for React has this component available for us and it is very easy to integrate. It can be used to turn an option on or off. We can create Instagram like button in
2 min read
How to Create Button in React-Native App ? React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read
How to create Like Button Using React JS ? This is a social media world and the like button is one of the main components of it. From social media accounts to shopping platforms like buttons are everywhere. In this tutorial, we'll explore how to create a dynamic button in a React application. The button will change its appearance based on di
4 min read
How to create Shopping Cart Button in ReactJS? A shopping cart button is one of the most used component in e-commerce websites or applications which allows users to add items to their cart . In this tutorial, we will learn how to create a shopping cart button in ReactJS using Material-UI. PrerequisitesJavaScript and JSXReact JSSteps to create Re
3 min read
How to Create a Responsive Layout with React Bootstrap ? Creating a responsive layout with React Bootstrap means designing a web page that adapts and looks good on various screen sizes and devices. React Bootstrap provides responsive components and grid system classes that help in organizing and scaling content effectively, ensuring a seamless user experi
3 min read
How to create a basic button in React Native ? In this article, we will learn how to create a basic button in React Native. To build a React Native app, we will use the Expo CLI, which provides a smoother experience for running your applications.ExpoIt is a framework and a platform for universal React applications. It is a set of tools and servi
5 min read