简体   繁体   English

如何使用反应功能组件添加或删除 class

[英]How can I add or remove class using react functional component

I am using react typescript. I am trying to add or remove an active class if I click any list item but the code is sometimes not working .我正在使用 React typescript。如果我单击任何列表项,我会尝试添加或删除活动的 class,但代码有时无法正常工作 So I want to update my code in a better way.所以我想以更好的方式更新我的代码。 can anyone help me to improve update my below function code?任何人都可以帮助我改进更新我下面的 function 代码吗?

Here is my function which I want to update这是我要更新的 function

   const list = document.querySelectorAll('.m-list') as NodeListOf<Element>

    function activeLink(e: any) {
        list.forEach((item) => item.classList.remove('active'))
        e.classList.add('active')
    }
    list.forEach(item => item.addEventListener('click', (e) => activeLink(item)))

Full code with HTML完整代码 HTML



export default function MobileNavigation() {
    const list = document.querySelectorAll('.m-list') as NodeListOf<Element>

    function activeLink(this: any) {
        list.forEach((item) => item.classList.remove('active'))
        // list.forEach((item) => item.classList.add('active'))
        this.classList.add('active')
    }
    list.forEach(item => item.addEventListener('click', activeLink))



    return <div className='mobile-navigation'>
        <ul>
            <li className='m-list active'>
                <a href="#">
                    <span className='m-icon'><i className="fa-solid fa-house"></i></span>
                    <span className='m-text'>Home</span>
                </a>
            </li>
            <li className='m-list'>
                <a href="#">
                    <span className='m-icon'><i className="fa-solid fa-shop"></i></span>
                    <span className='m-text'>Shop</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-store"></i></span>
                    <span className='m-text'>Vendors</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-address-card"></i></span>
                    <span className='m-text'>About us</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-headset"></i></span>
                    <span className='m-text'>Contact Us</span></a>
            </li>
            <div className="indicator"></div>
        </ul>
    </div>;
}

Your code is pure VanillaJS, not Typescript or React.您的代码是纯 VanillaJS,而不是 Typescript 或 React。 Try to read something about Typescript AND React, make example project and try - https://medium.com/codex/typescript-and-create-react-app-11bdebcbf763尝试阅读有关 Typescript 和 React 的内容,制作示例项目并尝试 - https://medium.com/codex/typescript-and-create-react-app-11bdebcbf763

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应功能组件中使用 querySelectorAll() 添加或删除 class - How can I add or remove class using querySelectorAll() in react functional component 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 如何使用 React Hooks 将此级联下拉 class 组件转换为功能组件? - How can I convert the this cascading dropdown class component to functional one using React Hooks? 单击按钮时,如何添加选定的 class 并从 React 功能组件中的所有其他按钮中删除? - On button click, how to add selected class and remove from all other buttons in React functional component? 如何编写返回 class 组件作为功能组件的 React 组件? - How can I write React component that returns a class component as a functional component? 如何反应才能识别它是 class 组件还是功能组件? - How react can Identify it's a class component or functional component? 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM