-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlive-session-14.12.2024-props.html
119 lines (99 loc) · 2.84 KB
/
live-session-14.12.2024-props.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// JSX (NOT pure JS, NOT pure XML, NOT HTML)
function Test(){
// We start reading (us and the JSX compiler) in JS:
let a = 1;
let b = 2;
// We switch (us and the compiler) to XML:
const markup = (
<h1>
XML <strong>Mode a b</strong>
a and b are just XML characters
</h1>
)
// We switch back to JS
console.log(a); // JS variables
console.log(b); // JS variables
// console.log(markup);
// IN JSX (JS) variables can hold XML.
// IN JSX, apart from booleans, strings, numbers, XML are
// also considered (special) values:
// return markup;
// When in XML Mode, { signifies switching to JS and } going back to XML
return (
<div>
{ a }
{ b }
<strong> { a + b }</strong>
{markup}
console.log("a", a); a
b
<p>Test</p>
</div>
)
}
function App() {
return (
<div>
<h1>Hello</h1>
<Test />
<FancyHeading color="red" background="black">
Python
</FancyHeading>
<FancyHeading color="hotpink">
JavaScript
</FancyHeading>
<FancyHeading>
<strong>TypeScript</strong>
<span>What?</span>
</FancyHeading>
</div>
);
}
// All props become parameters:
// <FancyHeading color background />
function FancyHeading(props){
console.log("FancyHeading()");
// Ideally, make a check on color and background and conditionally
// apply the values to the style object, e.g. when no color or background is set, use some default values:
const styling = { color: props.color, background: props.background }
const { children } = props;
return (
// <h2 style="color:red; background:black;">FancyHeading</h2>
<h2 style={
{ color: props.color }
}>
{children}
</h2>
);
}
// try {
// bomb();
// } catch (e) {
// console.log(e)
// }
// <ErrorBoundary>
// <Bomb />
// </ErrorBoundary>
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
</script>
</body>
</html>