Created
September 12, 2017 19:13
-
-
Save mikechabot/4da1a14534e11a18d03832258dc77437 to your computer and use it in GitHub Desktop.
Root Modal
This file contains hidden or 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
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