REACT - 单击时切换活动按钮类
我需要 2 个按钮,一个用于“左侧”,一个用于“右侧”。我制作了一个单独的组件(子文件夹),我在主文件中调用它。当用户单击某个按钮时,它必须变为“活动”,获得“square-round--active”类,以便它显示在 GUI 上。然而,目前我在弄清楚如何在 2 个单独的按钮上执行此操作时遇到问题,当时只有其中一个可以处于活动状态。在我当前的代码中,当单击其中任何一个按钮时,这些按钮就会相互切换为活动状态。我该如何解决这个问题?
const [isActive, setActive] = useState(false);
const toggleClass = () => {
setActive(!isActive);
};
<ul class="buttons-in-line no-bullets flex-line">
<li>
<button type="button" className={isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
onClick={toggleClass} >Left</button>
</li>
<li>
<button type="button" className={!isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
onClick={toggleClass} >Right</button>
</li>
</ul>
我现在将其作为占位符,但稍后我必须调整代码以与后端一起使用,将用户选择(左或右)作为布尔值发送。我在下面添加后端代码,如果有人知道如何将所有内容组合在一起,我将非常感激。
const inoAPI = new konfiguratorreact.client.api.InoAPI();
Getters:
inoAPI.getSestav().isOdpiranjeLevo(); // boolean
inoAPI.getSestav().isOdpiranjeDesno(); // boolean
Setters:
inoAPI.getSestav().setOdpiranjeDesno(callback(success, error)))
inoAPI.getSestav().setOdpiranjeLevo(callback(success, error)))
I need 2 buttons, one for the "left" and one for the "right" side. I've made a separate component (subfolder) that i am calling in the main file. When the user clicks on certain button it has to become "active", getting the "square-round--active" class so it shows on the GUI. However currently i am having problem figuring out how to do that on 2 separate buttons, only one of them can be active at the time. In my current code the buttons just switch active from one another when any of them is clicked. How can i solve this ?
const [isActive, setActive] = useState(false);
const toggleClass = () => {
setActive(!isActive);
};
<ul class="buttons-in-line no-bullets flex-line">
<li>
<button type="button" className={isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
onClick={toggleClass} >Left</button>
</li>
<li>
<button type="button" className={!isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
onClick={toggleClass} >Right</button>
</li>
</ul>
I am making this as a placeholder for now, but later on i have to adjust the code to work with the backend, sending the users pick (left or right) as boolean value. I am adding the backend code below, if anyone has any idea how to put everything together i would truly appreciate it.
const inoAPI = new konfiguratorreact.client.api.InoAPI();
Getters:
inoAPI.getSestav().isOdpiranjeLevo(); // boolean
inoAPI.getSestav().isOdpiranjeDesno(); // boolean
Setters:
inoAPI.getSestav().setOdpiranjeDesno(callback(success, error)))
inoAPI.getSestav().setOdpiranjeLevo(callback(success, error)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您目前没有任何方法可以跟踪单击了哪个按钮。像这样的东西应该有效。
父组件
You currently don't have any way of keeping track of which button is clicked. Something like this should work.
Parent Component