React close modal

A fiddle demonstrating how to close modal programatically

by Jan Klimo

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/classnames/2.1.3/dedupe.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>

<div class="container">
    <div class="row">
        <div id="app">
        </div>
    </div>
</div>

CSS

/* Optional theme */
body {
    margin: 10px;
}

.jumbotron {
    padding: 15px;
}

JavaScript 1.7

var Container = React.createClass({
 getInitialState(){
    return { showModal: false };
  },
  close(){
    this.setState({ showModal: false });
  },
  open(){
    this.setState({ showModal: true });
  },
  render() {
    return (
      <div className="jumbotron">
        <h3>
          This is the parent container.
        </h3>
        <p>
          <a className="btn btn-primary"
            href="#" role="button"
            onClick={this.open}>Show modal
          </a>
        </p>
        <Modal show={this.state.showModal} onHide={this.close}/>
      </div>
    );
  }
});

var Modal = React.createClass({
  render() {
    return <div 
    className={classNames('alert alert-success alert-dismissible', { hidden: !this.props.show}) } role="alert">
    <button type="button"
      className="close" onClick={this.props.onHide}>
      <span>&times;</span>
    </button>
    <strong>Well done!</strong> Click the button on the right to dismiss.
  </div>;
  }
});

React.render(<Container />, document.getElementById('app'));