Created
December 22, 2016 08:27
-
-
Save pindlebot/65d5f621a2667392aeda3c46199a1b08 to your computer and use it in GitHub Desktop.
How to implement a basic modal in ReactJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const display = { | |
display: 'block' | |
}; | |
const hide = { | |
display: 'none' | |
}; | |
class Modal extends React.Component { | |
constructor(props) { | |
super(props); | |
this.toggle = this.toggle.bind(this); | |
this.state = { | |
toggle: false | |
} | |
} | |
toggle(event) { | |
this.setState(prevState => ({ | |
toggle: !prevState.toggle | |
})); | |
} | |
render() { | |
var modal = []; | |
modal.push( | |
<div className="modal" style={this.state.toggle ? display : hide}> | |
<div className="modal-content"> | |
<h4>Modal Header</h4> | |
<p>A bunch of text</p> | |
</div> | |
<div className="modal-footer"> | |
<a className="btn" onClick={this.toggle}>Agree</a> | |
</div> | |
</div> | |
); | |
return ( | |
<div> | |
<a className="btn" onClick={this.toggle}>Modal</a> | |
{modal} | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<Modal />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read more about how to implement a reactJS modal here.