简体   繁体   English

ReactJs,如何将 class 名称添加到相同 div 的 Map

[英]ReactJs, How can I add class name to Map of same divs

this is my code i'm trying to add a class on click to a clicked div but i have more than 1 div have the same classname so when i click to any div the toggle class added to all divs not the one which i clicked what i want to know how can i specify the div which i want to add toggle class to it这是我的代码,我试图在单击时将 class 添加到单击的 div,但我有超过 1 个 div 具有相同的类名,因此当我单击任何 div 时,切换开关 class 添加到所有 div,而不是我单击的那个 div我想知道如何指定要向其添加切换 class 的 div

Code:代码:

    const [isActive, setActive] = useState(false);
    const toggleClass = () => {
        setActive(!isActive)
        console.log('sad')
    }


    return (
        <div className="command_wrapper">
            <div className="command_list">
                {
                    Commands.map((item, index) => {
                        return (
                            <div className="command" >
                                <div className="command_face" onClick={toggleClass}>
                                    <h1>{item.Command}</h1>
                                    <p>{item.Description}</p>
                                </div>
                                <div className={isActive ? "command_body" : "disapper"}>
                                    <div className="command_usage">
                                        <h1>Usage</h1>
                                        <p>{item.Usage}</p>
                                    </div>
                                    <div className="command_required">
                                        <h1>Required Permissions</h1>
                                        <p>{item.premissions}</p>
                                    </div>
                                </div>
                            </div>
                        )
                    })

                }
            </div>
        </div>
    ```

You can pass the item inside toggle function and verify if item passed is activated or just save the item clicked.您可以在 toggle function 中传递该项目,并验证传递的项目是否已激活或仅保存单击的项目。

const [itemActive, setItemActive] = useState();
const toggleClass = (item) => {
    setItemActive(item)
}


return (
    <div className="command_wrapper">
        <div className="command_list">
            {
                Commands.map((item, index) => {
                    return (
                        <div className="command" >
                            <div className="command_face" onClick={() => toggleClass(item)}>
                                <h1>{item.Command}</h1>
                                <p>{item.Description}</p>
                            </div>
                            <div className={item === itemActive ? "command_body" : "disapper"}>
                                <div className="command_usage">
                                    <h1>Usage</h1>
                                    <p>{item.Usage}</p>
                                </div>
                                <div className="command_required">
                                    <h1>Required Permissions</h1>
                                    <p>{item.premissions}</p>
                                </div>
                            </div>
                        </div>
                    )
                })

            }
        </div>
    </div>
```

If item has any unique property, you can save just then, like an ID or name.如果项目有任何独特的属性,您可以立即保存,例如 ID 或名称。 I think it will be better.我认为它会更好。

/*const [isActive, setActive] = useState(false);
const toggleClass = () => {
    setActive(!isActive)
    console.log('sad')
}*/
function handleClick(el){
  //toggle the class which indicates selection ex. 'active'
    el.classList.toggle('active');
 //other stuff todo

}
return (
    <div className="command_wrapper">
        <div className="command_list">
            {
                Commands.map((item, index) => {
                    return (
                        <div className="command" >
                            //destruct event objet{event.target}
                            <div onClick={({target})=>handleClick(target)}>
                                <h1>{item.Command}</h1>
                                <p>{item.Description}</p>
                            </div>
                         ....
                         ...
                        </div>
                    )
                })

            }
        </div>
    </div>

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

相关问题 如何检查具有相同ID的div数量的内容,并向包含特定文本的div添加一个类? - How can I check the content of number of divs with the same ID, and add a class to those that contain a certain piece of text? 如何使用jQuery影响同一类的8个div的单个div? - How can I affect a single div of 8 divs of a same class with jQuery? 找到第一个类,然后逐渐将类添加到具有相同名称的所有div - Find first class then gradually add class to all divs with same name 如何在 reactJS 中添加多个相同的字段形式? - How I can add multiple same fields form in reactJS? 如何使用每个 function ZD223E1439188E4578349D542 在两个相同的 class 名称上添加 class? - How can i add a class on two same class name using a each function jquery? 如何为具有相同名称的 div 显示相同的功能? - How do I display the same function for the divs with the same name? 如何分别在具有相同类名的div中接收AJAX(json)响应? - How to receive AJAX (json) response in a divs with same class name individually? 如何分别显示/隐藏具有相同类名的div? - How to Show/Hide divs with the same class name individually? 如何在具有相同 class 名称的 div 中查找最大子元素数 - How to find max number of children elements in divs with same class name 如何使具有相同类名的多个div可排序工作 - How to make sortable work with multi divs with the same class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM