[英]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.