Skip to content

Instantly share code, notes, and snippets.

@chadwilken
Created April 12, 2018 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chadwilken/eed0b9f547568155680c612f08add20e to your computer and use it in GitHub Desktop.
Save chadwilken/eed0b9f547568155680c612f08add20e to your computer and use it in GitHub Desktop.
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