Created
April 12, 2018 02:14
-
-
Save chadwilken/eed0b9f547568155680c612f08add20e to your computer and use it in GitHub Desktop.
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 { Controller } from 'stimulus'; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import AppContainer from 'components/core/AppContainer'; | |
export default class extends Controller { | |
connect() { | |
const Component = window.ReactComponents[this.data.get('component')]; | |
let props = this.data.get('props') || {}; | |
if (typeof props === 'string') { | |
props = JSON.parse(props); | |
} | |
ReactDOM.render( | |
<AppContainer> | |
<Component { ...props } /> | |
</AppContainer>, | |
this.element, | |
); | |
document.addEventListener('turbolinks:before-cache', this.unmountComponent); | |
} | |
disconnect() { | |
document.removeEventListener('turbolinks:before-cache', this.unmountComponent); | |
} | |
unmountComponent = () => { | |
ReactDOM.unmountComponentAtNode(this.element); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment