|
| 1 | +# Passing information |
| 2 | + |
| 3 | +When building a large code base with a number of different applications you will |
| 4 | +probably want to pass some information to them. |
| 5 | + |
| 6 | +We might need |
| 7 | +* User information |
| 8 | +* Information on where the app should mount |
| 9 | +* API access |
| 10 | +* Theme information |
| 11 | + |
| 12 | +## Custom props |
| 13 | + |
| 14 | +In a single-spa application we can hand arbitrary information to the lifecycle functions. |
| 15 | + |
| 16 | +```typescript |
| 17 | +registerApplication({ |
| 18 | + name: 'app1', |
| 19 | + app: () => import('./main.tsx').then(a => a.default), |
| 20 | + activeWhen: '/app1', |
| 21 | + customProps: { |
| 22 | + user: 'Fohan Automeit', |
| 23 | + apiToken: 'c0mpl1c473d$tr1n6', |
| 24 | + domElementGetter: () => document.getElementById('root')! |
| 25 | + } |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +with this we can now extend our application to make use of this: |
| 30 | + |
| 31 | +```typescript |
| 32 | +type CustomProps = { |
| 33 | + user: string; |
| 34 | + apiToken: string; |
| 35 | + domElementGetter: () => HTMLElement |
| 36 | +} |
| 37 | + |
| 38 | +let root: Root | null = null; |
| 39 | +const lifecycles: LifeCycles<CustomProps> = { |
| 40 | + bootstrap: () => Promise.resolve(), |
| 41 | + mount: ({ name, singleSpa, mountParcel, ...customProps }) => { |
| 42 | + console.debug('MOUNT', { |
| 43 | + name, |
| 44 | + singleSpa, |
| 45 | + mountParcel, |
| 46 | + customProps, |
| 47 | + }); |
| 48 | + root = ReactDOM.createRoot(customProps.domElementGetter()); |
| 49 | + root.render( |
| 50 | + <React.StrictMode> |
| 51 | + <App /> |
| 52 | + </React.StrictMode>, |
| 53 | + ); |
| 54 | + return Promise.resolve(); |
| 55 | + }, |
| 56 | + unmount: () => { |
| 57 | + root?.unmount(); |
| 58 | + return Promise.resolve(); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +export default lifecycles; |
| 63 | +``` |
| 64 | + |
| 65 | +In order to standardise this and make access easier we can make this info available to the rest |
| 66 | +of the app via the React context: |
| 67 | + |
| 68 | +```typescript |
| 69 | +import { createContext, useContext } from 'react'; |
| 70 | + |
| 71 | +export type CustomProps = { |
| 72 | + user: string; |
| 73 | + apiToken: string; |
| 74 | + domElementGetter: () => HTMLElement |
| 75 | +} |
| 76 | + |
| 77 | +const AppContext = createContext<CustomProps | null>(null); |
| 78 | + |
| 79 | +export const AppContextProvider = AppContext.Provider; |
| 80 | + |
| 81 | +export const useAppContext = () => { |
| 82 | + return useContext(AppContext); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +```tsx |
| 87 | +... |
| 88 | +root.render( |
| 89 | + <React.StrictMode> |
| 90 | + <AppContextProvider value={customProps}> |
| 91 | + <App /> |
| 92 | + </AppContextProvider> |
| 93 | + </React.StrictMode>, |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +With this added we can now easily get access to our user information anywhere in the app. |
| 98 | + |
| 99 | +```tsx |
| 100 | +const { user } = useAppContext()!; |
| 101 | +``` |
0 commit comments