ANTD开关反应 - 如何区分一个开关与另一个开关
我想使用ANTD开关在网站上实现简单的逻辑,以便我可以基本上检查一个开关,然后检查一个console.log(“已检查一个开关一个”)。但是我不知道如何区分一个切换与另一种切换。
here is my App.js
import React,{ useState } from 'react';
import { Switch } from 'antd';
import './App.css';
const App = () => {
const [toggle, setToggle] = useState() ;
const onChange = (checked) => {
console.log(`switch to ${checked}`);
}
return(
<div className="App">
<Switch onChange={onChange}></Switch>
<Switch onChange={onChange}></Switch>
</div>
);
}
export default App;
Right now there is no way how to distinguish the click of the on switch from click of the other switch.
I would like to implement simple logic on my website using antd switches so I can control my hue lights basically if switch one is checked then console.log("switch one is checked"). But i don't know how to distinguish one switch from another.
here is my App.js
import React,{ useState } from 'react';
import { Switch } from 'antd';
import './App.css';
const App = () => {
const [toggle, setToggle] = useState() ;
const onChange = (checked) => {
console.log(`switch to ${checked}`);
}
return(
<div className="App">
<Switch onChange={onChange}></Switch>
<Switch onChange={onChange}></Switch>
</div>
);
}
export default App;
Right now there is no way how to distinguish the click of the on switch from click of the other switch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以有两个状态值保存两个开关的状态。并确定检查了哪个开关,您可以使用两个不同的on Change回调来识别单击哪个开关。下面的演示代码:
You can have two state values saving the state of two switches. And to identify which switch was checked you can either use two different onChange callbacks to identify which switch was clicked. Demo code below:
您可以发送一些ID来管理此ID。您的开关代码将转换为这样的东西。
you can send some id to manage this. Your code for switch will be converted to something like this.