Super new to React and trying out something. I have 3 toggle switches on a single screen. Should I be using a separate state handler for every switch?
Right now I have used just one, but obviously this will not work as expected because isChecked
is being shared by all the three toggle switches.
const [isChecked, setIsChecked] = useState(false)
const data = [
['Sample Text 1', <ToggleSwitch showLabel={true} checked={isChecked} onChange={() => setIsChecked(!isChecked)}/>],
['Sample Text 2', <ToggleSwitch showLabel={true} checked={isChecked} onChange={() => setIsChecked(!isChecked)}/>],
['Sample Text 3', <ToggleSwitch showLabel={true} checked={isChecked} onChange={() => setIsChecked(!isChecked)}/>]
]
Can I use some sort of id to change the state of individual switches using the same handler(isClicked
)? Or is it recommended to use separate state handlers for every switch?
It really deepends what you want to achive.
The useState React hook controlls of one state. The useReducer can control multiple states which related.