PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / js / utils / bootstrap.tsx

bootstrap.tsx in Code Snippets 3.10.0, at js/utils/bootstrap.tsx

34 lines 876 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { createContext, useContext } from 'react'
2 import { createRoot } from 'react-dom/client'
3 import type { Context, FunctionComponent } from 'react'
4
5 export const loadComponent = (containerId: string, Component: FunctionComponent): void => {
6 const container = document.getElementById(containerId)
7
8 if (container) {
9 const root = createRoot(container)
10 root.render(<Component />)
11 } else {
12 console.error(`Could not find element #${containerId}.`)
13 }
14 }
15
16 export const createContextHook = <T, >(hookName: string): [
17 Context<T | undefined>,
18 () => T
19 ] => {
20 const Context = createContext<T | undefined>(undefined)
21
22 const useContextHook = (): T => {
23 const value = useContext(Context)
24
25 if (value === undefined) {
26 throw Error(`${hookName} can only be used within a corresponding context provider.`)
27 }
28
29 return value
30 }
31
32 return [Context, useContextHook]
33 }
34