React UI Components Documentation
ReactJS SwitchButton Component
The SwitchButton component for ReactJS lets the user toggle between checked and unchecked states.
Prerequisites
Refer to ReactJS Getting Started before you start with this help topic.
Configuration
The SwitchButton component for ReactJS requires the following imports.
import React from 'react';import ReactDOM from 'react-dom'; import JqxSwitchButton from 'jqwidgets-react/react_jqxswitchbutton.js';
Then we create our component class. Properties and methods are put as ReactJS props.
class App extends React.Component { render () { return ( <JqxSwitchButton ref='mySwitchButton' width={81} height={27} checked={true} /> ) }}
Finally we render our class in the desired HTML element:
ReactDOM.render(<App />, document.getElementById('app'));
Events Methods & Properties
In order to bind to any event, change any property or call any method, we need a reference to the component.
For that we use the ReactJS "ref" Callback Attribute:
<JqxSwitchButton ref='mySwitchButton' width={81} height={27}...../>
Now, when we have a reference, we need to call the desired event/property/method.
This is done in the componentDidMount() ReactJS Component Lifecycle method.
class App extends React.Component { componentDidMount() { //your logic } render () { return ( .... ) }};
Events
The checked event is triggered when the switch button is checked.
The following example demonstrates how to add an event listener:
componentDidMount (){ this.refs.mySwitchButton.on('checked', (event) => { //your logic }); }
Methods & Properties
This is how you call methods & props:
Every component have a method setOptions which accepts a object as an argument. This object contains component settings.
Every component also have a method getOptions which returns a object containing the component settings.
SwitchButton Examples
Overview
The following example demonstrates how to create a SwitchButton component.
Disabled SwitchButton
The following example demonstrates how to disable the SwitchButton component.