How To Change The Favicon In React JS?
Last Updated :
13 Oct, 2024
A favicon is a small icon that appears in the browser tab next to the page title. Changing the favicon in a React JS project can help improve the branding of your web application by displaying a custom icon that represents your business or website.
In this article, we will explore the following two different methods for changing the favicon in a React JS application.
Steps to Create React Application
Step 1: Create a react application by using this command
npx create-react-app <<My-Project>>
cd <<My-Project>>
Step 2: To Start the application run the following command.
npm start
Project Structure
Folder StructureDependencies
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Approach 1: Using the public Folder
In this approach, the meÂthod of changing the favicon in a ReactJS application entails placing a peÂrsonalized "favicon.ico" file in the "public" folder. By doing so, React automatically ideÂntifies and utilizes this file as the application's favicon.
Example: This example demonstrate how to modify index.html file in public folder to change the favicon in ReactJs
- Step 1: Create or obtain a custom favicon file (In public folder) or use any image from the internet that you want to use for your React application.
- Step 2: In your public/index.html file, make sure you have the following code inside the <head> section:
HTML
<!-- public/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Favicon Image URL -->
<link rel="icon" href=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_favicon.png" />
<!-- Other scripts and stylesheets -->
</head>
<body>
<div id="root"></div>
</body>
</html>
JavaScript
// App.js
import React from "react";
const containerStyle = {
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
minHeight: "100vh",
backgroundColor: "#ee",
color: "black",
textShadow: "2px 2px 4px rgba(0, 0, 0, 0.4)",
};
const headerStyle = {
textAlign: "center",
marginBottom: "20px",
};
const headingStyle = {
fontSize: "3rem",
marginBottom: "10px",
textTransform: "uppercase",
letterSpacing: "2px",
color: "green",
};
const paragraphStyle = {
fontSize: "1.5rem",
maxWidth: "600px",
lineHeight: "1.5",
};
const App = () => {
return (
<div style={containerStyle}>
<header style={headerStyle}>
<h1 style={headingStyle}>
Welcome to GeeksforGeeks
</h1>
<p style={paragraphStyle}>
A Computer Science portal for geeks. It
contains well-written, well-thought, and
well-explained computer science and
programming articles.
</p>
</header>
</div>
);
};
export default App;
Output
Change the Favicon in React JSApproach 2: Using a React Package
In this approach, the utilization of a ReÂact package known as "react-favicon" is employeÂd. By adopting this method, there is eÂnhanced flexibility in effeÂctively managing and updating the favicon based on various application stateÂs or user interactions.
Install the "react-favicon" package using npm or yarn:
npm install react-favicon
# or
yarn add react-favicon
Example: This example showcases a React application that enableÂs users to dynamically modify the websiteÂ's favicon with a simple button click.
JavaScript
import React, { useState } from "react";
import Favicon from "react-favicon";
const App = () => {
// Initialize the favicon URL state
// with the default favicon
const [faviconUrl, setFaviconUrl] = useState(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_favicon.png"
);
// Function to toggle the favicon
const toggleFavicon = () => {
// Check the current favicon and
// toggle to the opposite
setFaviconUrl(
(prevUrl) =>
prevUrl ===
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_favicon.png" ?
// Change to your second favicon file
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png"
:
// Change to your first favicon file
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_favicon.png"
);
};
const containerStyle = {
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
minHeight: "100vh",
backgroundColor: "#eee",
color: "black",
fontFamily: "Arial, sans-serif",
textShadow: "2px 2px 4px rgba(0, 0, 0, 0.4)",
};
const headerStyle = {
textAlign: "center",
marginBottom: "20px",
};
const headingStyle = {
fontSize: "2rem",
marginBottom: "10px",
textTransform: "uppercase",
color: "green",
};
const paragraphStyle = {
fontSize: "1.2rem",
maxWidth: "500px",
lineHeight: "1.5",
};
const buttonStyle = {
padding: "10px 20px",
fontSize: "1rem",
backgroundColor: "#0074D9",
color: "white",
border: "none",
cursor: "pointer",
borderRadius: "4px",
};
return (
<div style={containerStyle} className="App">
<Favicon url={faviconUrl} />
<header style={headerStyle}>
<h1 style={headingStyle}>
Welcome to GeeksforGeeks
</h1>
<p style={paragraphStyle}>
Click the button below to change the
favicon.
</p>
<button
onClick={toggleFavicon}
style={buttonStyle}
>
Toggle Favicon
</button>
</header>
</div>
);
};
export default App;
Output
Change the Favicon in React JS
Similar Reads
How to Change the Color of a Link in ReactJS ?
In this article, we'll see how we can change the color of a link in ReactJS. To change a link's color in React, add a CSS style to the link element using the style attribute in JSX code. Default link styles in HTML and CSS often include blue color and underlining. However, they may not align with yo
3 min read
How to Drawing Canvas in React.js ?
In this article, we are going to learn how we can add drawing canvas in ReactJs. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Prerequisites:NodeJS or NP
2 min read
How to create Header in React JS ?
The Header is an important element of a websiteâs design. It's the first impression of the website. It provides useful links to other areas of the website that the user may want to visit. In this article, we will create a functioning Header using React JS and Material UI.Prerequisites:NPM & Node
2 min read
How To Change Placeholder Color In ReactJS ?
In this article, we'll explore two different approaches to changing the placeholder color in ReactJS. To change the color of the placeÂholder text, the CSS ::placÂeholder pseudo-element is primarily utilized. This handy pseudo-element enables us to style the placeÂholder text within an input field.
3 min read
How to Set Box Shadow in React JS ?
The CSS property box-shadow is used to set box shadow in React. This can apply to any React.js component. The CSS box-shadow property creates shadow effects around an element's frame possible to set several effects at once by separating them with commas. A box shadow is defined by the element's X an
3 min read
How to add Chatbot in React JS Project ?
In today's digital world, chatbots have become an integral part of many websites and applications. They provide a convenient and efficient way to interact with users, answer their queries, and improve overall user experience. If you're working on a React JS project and want to incorporate a chatbot,
3 min read
How to use Skeleton Component in React JS ?
When data is not yet loaded, a Skeleton Component serves as a placeholder preview for the user. Material UI for React provides an easily integratable version of this component, and in React JS, the following approach can be employed to utilize the Skeleton Component.Prerequisites:React JSMaterial UI
2 min read
How to change the navbar color when you scroll in ReactJS ?
On scroll navbar color change in React highlights a sticky responsive navigation bar to navigate through web application pages. It provides an efficient way to navigate multiple pages in a single-page application. The following approach covers how to change the navbar color when you scroll through t
4 min read
How to Make Text Blink in ReactJS ?
In this article, we are going to learn about making a text blink in React. Making text blink in React refers to the process of creating a visual effect where text repeatedly alternates between being visible and invisible, creating a flashing or blinking appearance, often achieved through CSS animati
3 min read
How to set a Global Font Family in ReactJS ?
In React Setting a global font family means defining a specific font style to be used throughout a website or application, ensuring consistent typography across all elements and text content. When seÂtting a global font family, developers speÂcify a single font that will be universally applieÂd acro
5 min read