I am trying to display different things depending if a element is clicked or not in a navbar, in body.main App.js content:
const [showCatContent, setShowCatContent] = useState(false);
const toggleCatContent = () => {
setShowCatContent(!showCatContent);
};
console.log(showCatContent)
return (
<html><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script defer src="theme.js"></script>
<link rel="stylesheet" href="style.css" />
<link
href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700&display=swap"
rel="stylesheet"
/>
</head>
<body>
<Navbar toggleCatContent={toggleCatContent} />
<body>
<Navbar toggleCatContent={toggleCatContent} />
<main>
{showCatContent ? (
<p>This is my cat ahahhahahahaha</p>
) : (
<div>
<h1>CSS is Cool</h1>
Problem is in referenced navbar, when I try to update showCatInfo state when onClick event is happaning, to do so i call toggleCatContent(), but it causes it to execute it repetedly even if the element is not clicked
const Navbar = (props) => {
return (
<>
<nav className="navbar">
<ul className="navbar-nav">
<li className="logo" onClick={props.toggleCatContent()}>
I don't exactly understand why, isn't on click supposed to execute when u click on the said element?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/KfzOBUp
Comments
Post a Comment