I have a ToggleSwitch component in react which I have made using bootstrap-toggle
. But it is showing up as a checkbox and not as a toggle switch. Any help on this how to make it work?
ToggleSwitch.js
import React, { Component } from "react";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-toggle/css/bootstrap-toggle.css";
class ToggleSwitch extends Component {
render() {
return (
<div className="toggle-switch">
<input
type="checkbox"
data-toggle="toggle"
data-on="Ready"
data-off="Not Ready"
data-onstyle="success"
data-offstyle="danger"
/>
</div>
);
}
}
export default ToggleSwitch;
There is probably something wrong with the order in which JavaScript files are being called.
For CSS call the necessary files:
For JavaScript, call the files in this order:
Working snippet:
const element = (
<div className="toggle-switch">
<input
type="checkbox"
data-toggle="toggle"
data-on="Ready"
data-off="Not Ready"
data-onstyle="success"
data-offstyle="danger"
/>
</div>
);
ReactDOM.render(
element,
document.getElementById('app')
);
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet"/>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js"></script>