Skip to content

Commit f5d8e10

Browse files
committed
feat: add step 3
1 parent 6ea29a1 commit f5d8e10

19 files changed

Lines changed: 484 additions & 2 deletions

02 - the application/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:react-hooks/recommended',
8+
],
9+
ignorePatterns: ['dist', '.eslintrc.cjs'],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['react-refresh'],
12+
rules: {
13+
'react-refresh/only-export-components': [
14+
'warn',
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
<script src="https://cdn.jsdelivr.net/npm/systemjs@6.12.1/dist/system.min.js"></script>
9+
</head>
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="/src/root-config.ts"></script>
13+
</body>
14+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "single-spa-workshop-03",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"react": "^18.2.0",
14+
"react-dom": "^18.2.0",
15+
"single-spa": "^5.9.5"
16+
},
17+
"devDependencies": {
18+
"@types/react": "^18.2.15",
19+
"@types/react-dom": "^18.2.7",
20+
"@typescript-eslint/eslint-plugin": "^6.0.0",
21+
"@typescript-eslint/parser": "^6.0.0",
22+
"@vitejs/plugin-react": "^4.0.3",
23+
"eslint": "^8.45.0",
24+
"eslint-plugin-react-hooks": "^4.6.0",
25+
"eslint-plugin-react-refresh": "^0.4.3",
26+
"typescript": "^5.0.2",
27+
"vite": "^4.4.5"
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useState } from 'react'
2+
import reactLogo from './assets/react.svg'
3+
import viteLogo from '/vite.svg'
4+
import './App.css'
5+
import { useAppContext } from './app-context.ts';
6+
7+
function App() {
8+
const [count, setCount] = useState(0);
9+
const { user } = useAppContext()!;
10+
11+
return (
12+
<>
13+
<div>
14+
<a href="https://vitejs.dev" target="_blank">
15+
<img src={viteLogo} className="logo" alt="Vite logo" />
16+
</a>
17+
<a href="https://react.dev" target="_blank">
18+
<img src={reactLogo} className="logo react" alt="React logo" />
19+
</a>
20+
</div>
21+
<h1>{user}</h1>
22+
<div className="card">
23+
<button onClick={() => setCount((count) => count + 1)}>
24+
count is {count}
25+
</button>
26+
<p>
27+
Edit <code>src/App.tsx</code> and save to test HMR
28+
</p>
29+
</div>
30+
<p className="read-the-docs">
31+
Click on the Vite and React logos to learn more
32+
</p>
33+
</>
34+
)
35+
}
36+
37+
export default App
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createContext, useContext } from 'react';
2+
3+
export type CustomProps = {
4+
user: string;
5+
apiToken: string;
6+
domElementGetter: () => HTMLElement
7+
}
8+
9+
const AppContext = createContext<CustomProps | null>(null);
10+
11+
export const AppContextProvider = AppContext.Provider;
12+
13+
export const useAppContext = () => {
14+
return useContext(AppContext);
15+
}
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)