You need to manage a state for the toggle switch through the external button click.
This is a reference code that should get you started.
import React from "react";
export default function App() {
const [isOn, setIsOn] = React.useState(false);
return (
<>
<input
type="checkbox"
checked={isOn}
onClick={(e) => setIsOn(e.target.val)}
/>
<br />
<button onClick={() => setIsOn((prevState) =>
!prevState)}>Toggle</button>
</>
);
}
This code is available here: https://codesandbox.io/s/hungry-knuth-55eh0?file=/src/App.js
Answer from MMH on Stack OverflowVideos
Why should you choose Syncfusion React Toggle Switch Button?
Where can I find the Syncfusion React Toggle Switch Button demo?
How do I get started with Syncfusion React Toggle Switch Button?
You need to manage a state for the toggle switch through the external button click.
This is a reference code that should get you started.
import React from "react";
export default function App() {
const [isOn, setIsOn] = React.useState(false);
return (
<>
<input
type="checkbox"
checked={isOn}
onClick={(e) => setIsOn(e.target.val)}
/>
<br />
<button onClick={() => setIsOn((prevState) =>
!prevState)}>Toggle</button>
</>
);
}
This code is available here: https://codesandbox.io/s/hungry-knuth-55eh0?file=/src/App.js
As the toggle switch is a component of my employer, I just use checkbox to replace in this example.
I think I can code for my question (as you can see my code shown as below) and I found why the toggle switch cannot be turned off by a button... It is because there is a bug in the toggle switch component of my employer and then I have eventually fixed the bug.
Anyway, let me show the code for this question (even it is not the answer for the fixing the bug that I really need):
import React from "react";
import "./styles.css";
export default function App() {
const [isOn, setIsOn] = React.useState(
{ a: false , b: false, c: false }
);
const handleInputA = () => {
setIsOn({ ...isOn, a: !isOn.a });
}
const handleInputB = (inputLabel) => {
setIsOn({ ...isOn, b: !isOn.b });
}
const handleInputC = (inputLabel) => {
setIsOn({ ...isOn, c: !isOn.c });
}
return (
<>
<input
type="checkbox"
checked={isOn.a}
onClick={handleInputA}
/>a
<br />
<input
type="checkbox"
checked={isOn.b}
onClick={handleInputB}
/>b
<br />
<input
type="checkbox"
checked={isOn.c}
onClick={handleInputC}
/>c (The one that I want to do for this question.)
<br />
{ isOn.c &&
<button onClick={handleInputC}>Unselect C</button>
}
</>
);
}
You may check to see the effect if you are interested: https://codesandbox.io/s/toggleswitch-tpkti
» npm install react-switch
You can try this. Thank you.
export default class App extends Component {
state = {
isOn: false,
}
handleClick() {
this.setState(preState => ({ isOn: !preState.isOn }))
}
render() {
const { isOn } = this.state;
return (
<button onClick={this.handleClick}>
{isOn ? "On" : "Off"}
</button>
);
}
}
You can do something like this:
export default class App extends Component {
state = {
status: false,
switchButton: "Off"
}
handleClick() {
this.setState({
status: !this.state.status,
switchButton: !this.state.status ? 'ON' : 'OFF'
})
}
render() {
return (
<button onClick={()=> this.handleClick()}>
{this.state.switchButton}
</button>
);
}
}
The initial value of state is false. So, after you do the first click, you set state to !state, which makes it true (because !false equals true). On the second click, it will become false because state was true and setting it to !state made it false (!true equals false). The exclamation mark (!) is read as not.
By the way, I recommend setting the state like this:
setState(prev => !prev);
Hope this helps.
In react when you setState and want to use the previous value as the input you should do this:
setState(pre => !pre)
Here we get the current value by using pre in the useState function. This should work perfectly.