功能在点击时没有触发

huangapple go评论53阅读模式
英文:

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 &#39;react&#39;;
import &#39;../css-files/navbar.css&#39;;
import logo from &#39;../assets/svg/logo.svg&#39;;

export default function Navbar() {

    function setCurrentActive() {
        console.log(&quot;works&quot;)
    } 
    
    return (
            &lt;div className=&quot;Navbar&quot; id=&quot;myNavBar&quot;&gt;
            &lt;img className=&quot;logo&quot; src={logo} alt=&#39;&#39;/&gt;
            &lt;ul className=&quot;links&quot; id=&quot;Links&quot;&gt;
                &lt;NavButton id={1}label=&quot;ACASĂ&quot; onClick={setCurrentActive}/&gt;
                &lt;NavButton id={2}label=&quot;ACUSTICE&quot;/&gt;
                &lt;NavButton id={3}label=&quot;CLASICE&quot;/&gt;
                &lt;NavButton id={4}label=&quot;ELECTRICE&quot;/&gt;
            &lt;/ul&gt;
            &lt;a href=&quot;/&quot; className=&quot;icon&quot;&gt;
                &lt;img src=&quot;../assets/png/menu.png&quot; alt=&quot;&quot;/&gt;
            &lt;/a&gt;
            &lt;/div&gt;
    );
}

function NavButton(props) {
    const [name, setName] = useState(props.id === 1 ? &quot;active&quot; : &quot;&quot;);

    function changeName() {
        if (name === &quot;&quot;)
            setName(&quot;active&quot;);
        else
            setName(&quot;&quot;);
    }

    return (
        &lt;li className={name} onClick={changeName}&gt;
            {props.label}
        &lt;/li&gt;   
    )
}

答案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 &#39;react&#39;;
import &#39;../css-files/navbar.css&#39;;
import logo from &#39;../assets/svg/logo.svg&#39;;
export default function Navbar() {
function setCurrentActive() {
console.log(&quot;works&quot;)
} 
return (
&lt;div className=&quot;Navbar&quot; id=&quot;myNavBar&quot;&gt;
&lt;img className=&quot;logo&quot; src={logo} alt=&#39;&#39;/&gt;
&lt;ul className=&quot;links&quot; id=&quot;Links&quot;&gt;
&lt;NavButton id={1}label=&quot;ACASĂ&quot; onClick={setCurrentActive}/&gt;
&lt;NavButton id={2}label=&quot;ACUSTICE&quot;/&gt;
&lt;NavButton id={3}label=&quot;CLASICE&quot;/&gt;
&lt;NavButton id={4}label=&quot;ELECTRICE&quot;/&gt;
&lt;/ul&gt;
&lt;a href=&quot;/&quot; className=&quot;icon&quot;&gt;
&lt;img src=&quot;../assets/png/menu.png&quot; alt=&quot;&quot;/&gt;
&lt;/a&gt;
&lt;/div&gt;
);
}
function NavButton(props) {
const [name, setName] = useState(props.id === 1 ? &quot;active&quot; : &quot;&quot;);
function changeName() {
if (props.onClick) props.onClick();
if (name === &quot;&quot;)
setName(&quot;active&quot;);
else
setName(&quot;&quot;);
}
return (
&lt;li className={name} onClick={changeName}&gt;
{props.label}
&lt;/li&gt;   
)
}

huangapple
  • 本文由 发表于 2023年2月27日 04:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定