import React from "react";
function getFaviconEl() {
return document.getElementById("favicon");
}
function App() {
const handleGoogle = () => {
const favicon = getFaviconEl(); // Accessing favicon element
favicon.href = "https://www.google.com/favicon.ico"; };
const handleYoutube = () => {
const favicon = getFaviconEl();
favicon.href = " https://s.ytimg.com/yts/img/favicon-vfl8qSV2F.ico"; };
return (
<div className="App">
<h1>Dynamically changing favicon</h1>
<button onClick={handleGoogle}>Google</button> <button onClick={handleYoutube}>YouTube</button> </div>
);
}
export default App;
1. Open the react app in your favorite code editor.
2. Navigate to the public folder and delete the favicon.ico file.
3. Now, add a new favicon inside the public folder.