javascript - Using React with Bootstrap Toggle -
i want use bootstrap toggle inside react component. regular checkbox shown instead of styled element. how fixed?
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- bootstrap core css --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> <script src="https://fb.me/react-0.13.1.js"></script> <script src="https://fb.me/jsxtransformer-0.13.1.js"></script> <script type="text/jsx"> var wordcheckbox = react.createclass({ render: function() { return ( <div classname="checkbox"> <label> <input type="checkbox" data-toggle="toggle" data-on="on" data-off="off" /> option1 </label> </div> ); } }); react.render( <wordcheckbox />, document.getelementbyid('content') ); </script> <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script> </head> <body> <!--doesn't work. checkbox instead of toogle shown--> <div id="content"> </div> <!--works--> <div class="checkbox"> <label> <input type="checkbox" data-toggle="toggle" data-on="on" data-off="off"> <span>option2</span> </label> </div> <!--works--> <div class="checkbox" data-reactid=".0"> <label data-reactid=".0.0"> <input type="checkbox" data-toggle="toggle" data-on="on" data-off="off" data-reactid=".0.0.0"> <span data-reactid=".0.0.1">option3</span> </label> </div> </body> </html>
based on their docs
you need wire plugin's functionality when react component mounts (when spits out html dom)
add componentdidmount
lifecycle hook , give input ref
attribute accomplish this.
var wordcheckbox = react.createclass({ componentdidmount: function() { $( this.refs.toggleinput.getdomnode() ).bootstraptoggle(); } ... render: function() { ... <input ref="toggleinput" type="checkbox" data-toggle="toggle" data-on="on" data-off="off" />
Comments
Post a Comment