Last active
April 13, 2022 17:52
-
-
Save sb8244/9d60231e1d13c177ec8de20d22b9ec8d to your computer and use it in GitHub Desktop.
Example hooks + React mounter for LiveView application
This file contains 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
export const SpaceLayoutEditor = { | |
mounted() { | |
// I use webpack chunks to reduce LiveView entry file size | |
import(/* webpackChunkName: "space-layout-editor-lv" */ '../entry/space-layout-editor-lv').then((mounter) => { | |
this.unmountComponent = mounter.default(this.el.id, { | |
editorOpts: this.editorOpts() | |
}) | |
}).catch(console.error) | |
}, | |
destroyed() { | |
if (!this.unmountComponent) { | |
console.error('SpaceLayoutEditor unmountComponent not set') | |
return | |
} | |
this.unmountComponent(this.el) | |
}, | |
} |
This file contains 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 { unmountComponentAtNode } from 'react-dom' | |
import { AppOpts } from '../space_editor/App' | |
import mountEditor from '../space_editor/mount' | |
export default function mount(id: string, opts: AppOpts) { | |
mountEditor(id, opts) | |
return (el: Element) => { | |
if (!unmountComponentAtNode(el)) { | |
console.warn('unmount failed', el) | |
} | |
} | |
} |
This file contains 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 { render } from 'react-dom' | |
// This is just illustrating that you can include additional CSS in your App, just like you normally would | |
// Again, the application is self-contained as much as possible | |
import 'react-grid-layout/css/styles.css' | |
import 'react-resizable/css/styles.css' | |
// It doesn't really matter what this App is. It's a plain ol' React component | |
import App from './App' | |
export default function mount(id: string, opts: any) { | |
const rootElement = document.getElementById(id) | |
render( | |
<App {...opts} />, | |
rootElement | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment