Skip to content

Instantly share code, notes, and snippets.

@mikechabot
Created September 12, 2017 19:13
Show Gist options
  • Save mikechabot/4da1a14534e11a18d03832258dc77437 to your computer and use it in GitHub Desktop.
Save mikechabot/4da1a14534e11a18d03832258dc77437 to your computer and use it in GitHub Desktop.
Root Modal
import React from 'react';
import { connect } from 'react-redux';
const MODAL_COMPONENTS = {
'MODAL_ONE': require('./modal-one').ModalOne,
'MODAL_TWO': require('./modal-one').ModalTwo
};
class RootModal extends React.Component {
constructor (props) {
super(props);
this.state = {};
}
componentDidMount () {
this._initState(this.props);
}
componentWillReceiveProps (nextProps) {
this._initState(nextProps);
}
render () {
const { modalType, modalProps } = this.state;
if (!modalType) {
return <span />;
}
const ModalComponent = MODAL_COMPONENTS[modalType];
if (!ModalComponent) {
console.warn('Unmapped modal type: ', modalType);
return <span />;
}
return <ModalComponent { ...modalProps } />;
}
_initState (props) {
this.setState(props.modal);
}
}
export default connect(
(state) => ({ modal: state.modal })
)(RootModal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment