Skip to content

Instantly share code, notes, and snippets.

@danprince
Created August 9, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danprince/40e41046f64184820a90e269cde85b9d to your computer and use it in GitHub Desktop.
Save danprince/40e41046f64184820a90e269cde85b9d to your computer and use it in GitHub Desktop.
Function-as-child Modals
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