I need to make React responsive navbar. I find some that work in HTML, but not in React. I think problem is in getElementById, because React doesn't support it.
import React from "react";
import { Link } from "react-router-dom";
import "./index.css";
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
function Header() {
return (
<nav>
<div className="topnav" id="myTopnav">
<a href="#home" className="active">
Home
</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" className="icon" onclick={myFunction}>
<i className="fa fa-bars"></i>
</a>
</div>
</nav>
);
}
export default Header;
This is the CSS my project is using:
.topnav {
overflow: hidden;
background-color: black;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
How should I update my react code so that the responsive menu appears when clicking the icon?