Created
August 9, 2016 09:18
-
-
Save danprince/40e41046f64184820a90e269cde85b9d to your computer and use it in GitHub Desktop.
Function-as-child Modals
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
class Modal extends Component { | |
constructor() { | |
super(); | |
this.state = { showing: true }; | |
this.close = this.close.bind(this); | |
} | |
close() { | |
this.setState({ showing: false }); | |
} | |
render() { | |
if(!this.state.showing) { | |
return null; | |
} | |
// pass methods to let props deal with | |
const content = this.props.children(this.close); | |
return ( | |
<div className='overlay'> | |
<div className='modal'> | |
<a onClick={this.close}>X</a> | |
{content} | |
</div> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<Modal> | |
{(close) => | |
<a onClick={close}>Clicking me will close the modal</a> | |
} | |
</Modal>, | |
document.getElementById('root') | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment