score:0
Accepted answer
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:
import React,{ useState } from 'react';
import { Switch } from 'antd';
import './App.css';
const App = () => {
const [toggleSwitchOne, setToggleSwitchOne] = useState(false) ;
const [toggleSwitchTwo, setToggleSwitchTwo] = useState(false) ;
const onChangeSwitchOne = (checked) => {
console.log(`switch 1 to ${checked}`);
setToggleSwitchTwo(!toggleSwitchOne)
}
const onChangeSwitchTwo = (checked) => {
console.log(`switch 2 to ${checked}`);
setToggleSwitchTwo(!toggleSwitchTwo)
}
return(
<div className="App">
<Switch checked={toggleSwitchOne} onChange={onChangeSwitchOne}></Switch>
<Switch checked={toggleSwitchTwo} onChange={onChangeSwitchTwo}></Switch>
</div>
);
}
export default App;
score:0
you can send some id to manage this. Your code for switch will be converted to something like this.
import React,{ useState } from 'react';
import { Switch } from 'antd';
import './App.css';
const App = () => {
const [toggle, setToggle] = useState() ;
const onChange = (id,checked) => {
console.log(`switch to ${id} is ${checked}`);
}
return(
<div className="App">
<Switch onChange={(checked)=>onChange("ab",checked)}></Switch>
<Switch onChange={(checked)=>onChange("cd",checked)}></Switch>
</div>
);
}
export default App;
Source: stackoverflow.com
Related Query
- antd switch react - how to distinguish one switch from another
- how to navigate from one page to another in react js?
- How to from one component set state another component in react native?
- How to pass data from one component to another in React or React-Redux?
- React Microfronends: How do I pass authentication and other information from one Front end app to another Front end app?
- How to refresh data of one component from another component in react js
- React - how to populate one select menu based on a value of another select menu using data from a local .json file
- How to pass value of a const from one .js file to another in React JS?
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- How to set one component's state from another component in React
- How can i pass a state from one react route to another react route?
- React - How to Populate one Dropdowns based on selection from another Dropdown by Passing State as props
- How to navigate from one component to another by clicking a button using React router
- How to give props from one page to another with react Router
- How to pass a Json data from one folder to another folder in react
- How to send result data from one page to another page in React
- How to pass a prop from one component to another when using react router link
- How to pass data from one variable in props to another in React app?
- how to pass the data from one component to another in react js
- How can I stop the Select component from closing when I clicked one of its item(Input element) - react antd
- How can I pass props from one component to another using Link and router in react
- how to pass data from one container to another container in react router
- How to pass state from one component to another in React js?
- How to pass data from one component to another component in onchange using React js
- How to transfer data from one parent component to another parent component in react using router / router params
- How to pass a variable from one method to another in React
- React - How can I pass API data from one component to another in a different js file?
- How do I pass data from one sibling to another using React Router?
- How to pass array as result from one functional component to another in React
- How to switch from one screen to another after a certain time in ReactJS?
More Query from same tag
- Custom react datepicker with typescript
- React hooks: listen to only one state change but depend on several other states to update a new state?
- React Spring and Styled Components
- How to filter dates with a given range from a start and end date?
- Dispatching reducers with different action types
- Is there a way to work with an Object in Object typescript?
- Why does my code type get a warning? "warning Missing return type on function"
- How to change reactid suffix in react component
- I have a light/dark theme toggle react component that I'm trying to improve
- empty cart, firebase-firestore react-hooks
- React Hooks and Socket.IO compare values before update
- React / Redux - Get Details from Url ID
- componentDidUpdate error is React limits the number of nested updates to prevent infinite loops how to fix this issue
- How render a value for input field in expressJS
- How to access the values of a nested object in an array, it keeps saying Uncaught TypeError: Cannot read properties of undefined
- set Highstock xAxis range within options object
- Updated state not rendering in child component
- reactjs - phantomjs-node issues with "fs" module
- React checkbox if checked add value to array
- React Pagination : How to Load second page data on Click
- State does not update in async function
- Why does Axios get call return a 404 error with basic setup?
- How to use a promised function in a react app
- set and get date and time in localStorage in react datepicker
- React useState not updating value in async function
- How to properly handle unauthenticated users and requests in AWS Amplify/AppSync React apps?
- Should return run before or after useEffect?
- electron & react: No resource with given URL found, DevTools failed to load SourceMap
- Making dynamic canvas a background to a div using React
- React Router Redirect not Rendering