英文:
Function not triggering on click
问题
以下是您要翻译的代码部分:
The function is not triggering when I press on the NavButton component.
I'm new to React.js and I'm trying to call the setCurrentActive function when NavButton gets clicked, but nothing happens. Am I doing it wrong?
import React, { useState } from 'react';
import '../css-files/navbar.css';
import logo from '../assets/svg/logo.svg';
export default function Navbar() {
function setCurrentActive() {
console.log("works")
}
return (
<div className="Navbar" id="myNavBar">
<img className="logo" src={logo} alt=''/>
<ul className="links" id="Links">
<NavButton id={1} label="ACASĂ" onClick={setCurrentActive}/>
<NavButton id={2} label="ACUSTICĂ"/>
<NavButton id={3} label="CLASICĂ"/>
<NavButton id={4} label="ELECTRICĂ"/>
</ul>
<a href="/" className="icon">
<img src="../assets/png/menu.png" alt=""/>
</a>
</div>
);
}
function NavButton(props) {
const [name, setName] = useState(props.id === 1 ? "active" : "");
function changeName() {
if (name === "")
setName("active");
else
setName("");
}
return (
<li className={name} onClick={changeName}>
{props.label}
</li>
)
}
英文:
The function is not triggering when I press on the NavButton component.
I'm new to React.js and I'm trying to call the setCurrentActive function when NavButton gets clicked, but nothing happens. Am I doing it wrong?
import React, { useState } from 'react';
import '../css-files/navbar.css';
import logo from '../assets/svg/logo.svg';
export default function Navbar() {
function setCurrentActive() {
console.log("works")
}
return (
<div className="Navbar" id="myNavBar">
<img className="logo" src={logo} alt=''/>
<ul className="links" id="Links">
<NavButton id={1}label="ACASĂ" onClick={setCurrentActive}/>
<NavButton id={2}label="ACUSTICE"/>
<NavButton id={3}label="CLASICE"/>
<NavButton id={4}label="ELECTRICE"/>
</ul>
<a href="/" className="icon">
<img src="../assets/png/menu.png" alt=""/>
</a>
</div>
);
}
function NavButton(props) {
const [name, setName] = useState(props.id === 1 ? "active" : "");
function changeName() {
if (name === "")
setName("active");
else
setName("");
}
return (
<li className={name} onClick={changeName}>
{props.label}
</li>
)
}
答案1
得分: 0
NavButton
是一个自定义组件,因此它没有onClick
监听器,所以如果您要将任何属性传递给这个"自定义"组件,您需要处理它。
要处理这个,您只需要在原生的onClick按钮监听器中传递if (props.onClick) props.onClick();
。
import React, { useState } from 'react';
import '../css-files/navbar.css';
import logo from '../assets/svg/logo.svg';
export default function Navbar() {
function setCurrentActive() {
console.log("works")
}
return (
<div className="Navbar" id="myNavBar">
<img className="logo" src={logo} alt=''/>
<ul className="links" id="Links">
<NavButton id={1} label="ACASĂ" onClick={setCurrentActive}/>
<NavButton id={2} label="ACUSTICE"/>
<NavButton id={3} label="CLASICE"/>
<NavButton id={4} label="ELECTRICE"/>
</ul>
<a href="/" className="icon">
<img src="../assets/png/menu.png" alt=""/>
</a>
</div>
);
}
function NavButton(props) {
const [name, setName] = useState(props.id === 1 ? "active" : "");
function changeName() {
if (props.onClick) props.onClick();
if (name === "")
setName("active");
else
setName("");
}
return (
<li className={name} onClick={changeName}>
{props.label}
</li>
)
}
英文:
NavButton
is a custom component, so it doesn't have an onClick
listener, so if you are passing any props to the' custom' component, you need to handle it.
To handle that, you just need to pass if (props.onClick) props.onClick();
in your native onClick button listener.
import React, { useState } from 'react';
import '../css-files/navbar.css';
import logo from '../assets/svg/logo.svg';
export default function Navbar() {
function setCurrentActive() {
console.log("works")
}
return (
<div className="Navbar" id="myNavBar">
<img className="logo" src={logo} alt=''/>
<ul className="links" id="Links">
<NavButton id={1}label="ACASĂ" onClick={setCurrentActive}/>
<NavButton id={2}label="ACUSTICE"/>
<NavButton id={3}label="CLASICE"/>
<NavButton id={4}label="ELECTRICE"/>
</ul>
<a href="/" className="icon">
<img src="../assets/png/menu.png" alt=""/>
</a>
</div>
);
}
function NavButton(props) {
const [name, setName] = useState(props.id === 1 ? "active" : "");
function changeName() {
if (props.onClick) props.onClick();
if (name === "")
setName("active");
else
setName("");
}
return (
<li className={name} onClick={changeName}>
{props.label}
</li>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论