简体   繁体   English

单击按钮时,如何添加选定的 class 并从 React 功能组件中的所有其他按钮中删除?

[英]On button click, how to add selected class and remove from all other buttons in React functional component?

I am currently learning React and I have a functional component for a menu.我目前正在学习 React,并且我有一个菜单的功能组件。 When a value is clicked I want to add the 'choice-selected' class to visually show it has been selected and make sure no other button has the class.单击一个值时,我想添加“选择选择” class 以直观地显示它已被选中,并确保没有其他按钮具有 class。 Is there a way to create this behavior without creating a separate useState hook for every selection?有没有一种方法可以在不为每个选择创建单独的 useState 挂钩的情况下创建这种行为?

Here is my code:这是我的代码:

function SortMenu(props){
    const [clickedDay, setClickedDay] = useState('');
    const [clickedMonth, setClickedMonth] = useState('');
    const [clickedYear, setClickedYear] = useState('');
    const choice = (value) =>{
        props.setSort(value);

        switch(value){
            case 'day':
                setClickedDay( (prevVal) => {
                    prevVal =='' ? setClickedDay('choice-selected'): setClickedDay('');
                    setClickedMonth('');
                    setClickedYear('');
                });
                break;
            case 'month':
                setClickedMonth( (prevVal) => {
                    prevVal =='' ? setClickedMonth('choice-selected'): setClickedMonth('');
                    setClickedDay('');
                    setClickedYear('');
                })
                break;
            case 'year':
                setClickedYear( (prevVal) => {
                    prevVal =='' ? setClickedYear('choice-selected'): setClickedYear('');
                    setClickedDay('');
                    setClickedMonth('');
                })
                break;

        }
    }
    return(
    <nav className="sortmenu">
        <div className={`${clickedDay} choice`} onClick={() => {choice("day");}}>Day</div>
        <div className={`${clickedMonth} choice`} onClick={() => {choice("month")}}>Month</div>
        <div className={`${clickedYear} choice`} onClick={() => {choice("year")}}>Year</div>
    </nav>
    )
}

Currently my code does work, but if I need to add more choices I have to not only add another case, but add the new choices in the existing cases as well.目前我的代码确实有效,但如果我需要添加更多选择,我不仅要添加另一个案例,还要在现有案例中添加新选择。 I am wondering if there is an easier or better way to implement this behavior?我想知道是否有更简单或更好的方法来实现这种行为?

Here is a working example of the solution I described in the comments.这是我在评论中描述的解决方案的一个工作示例。

 const { useState } = React; const SortMenu = () => { const [clickedChoice, setClickedChoice] = useState(''); const choice = (value) => { setClickedChoice(value); }; return ( <nav> <div className={`${clickedChoice === 'day'? 'choice-selected': ''} choice`} onClick={() => choice('day')} >Day</div> <div className={`${clickedChoice === 'month'? 'choice-selected': ''} choice`} onClick={() => choice('month')} >Month</div> <div className={`${clickedChoice === 'year'? 'choice-selected': ''} choice`} onClick={() => choice('year')} >Year</div> </nav> ) } ReactDOM.render(<SortMenu/>, document.querySelector('#root'));
 .choice { border: 1px solid black; padding: 2px; }.choice-selected { background-color: pink; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> Click an option to select it. <div id="root"></div>

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

相关问题 在反应js中单击按钮时从class组件导航到功能组件 - Navigate from class component to functional component on button click in react js 如何在单击一次按钮时呈现另一个反应组件? (功能组件) - How to render another react component on a single button click? (Functional Component) 如何在反应按钮中添加 class 和删除 class? - how to add class and remove class from buttons in react? 当我单击香草javascript中的其他按钮时,从所有其他按钮中删除ACTIVE类 - Remove ACTIVE classes from all other buttons when I click an other button in vanilla javascript 添加课程并从所选按钮中删除课程 - add class and remove class from selected button 单击按钮时调用功能组件反应 - Call functional Component on button click in react 如何在反应功能组件中的 AgGrid Cell 中添加按钮和下拉菜单 - How to add a button and a dropdown in AgGrid Cell in react Functional Component 单击将class添加到具有特定类的元素,并将其从具有相同类的所有其他元素中删除 - On click add class to element with certain class and remove it from all other elements with same class 如何将功能组件中的道具添加到 class 组件中? - How to add a prop from a functional component into a class component? 单击按钮时如何删除Antd select / multiselect组件中的选定值 - How to remove selected values in Antd select/Multiselect component on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM