-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_npm1.html
224 lines (181 loc) · 8.03 KB
/
no_npm1.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Adding React to html webPage</title>
<style> /* Internal CSS */
body {background-color: #E4E4E4; font-size: 10pt; font-family: 'Arial','Segoe UI','Calibri' }
h1, h2 {color: #302009; margin: 10px;}
p {margin: 6px; }
.BabelBox {background-color: #c5ddfc; padding: 8px; margin: 6px;
width: 450px; border-radius: 6px; border: 2px solid #709ad9}
</style>
<!--Main CORE lib React -->
<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://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
</head>
<body>
<h2>Simple React elements in one .html file</h2>
<p>This page demonstrates using React with no build tooling, no use NPM.
React elements are composed of [Class Component (state full)] and
[Function Component (state less)]. The [Function Component] uses the Hook technique.
</p>
<ul>
<li>React without NPM;</li>
<li>Project in one .html file;</li>
<li>Defined and used 4 React components;</li>
<li>Used Class_Component, Function_Component and components with JSX syntax;</li>
<li>Adding React elements to a regular html webpage;</li>
<li>React ver.18 sources are linked online from the internet;</li>
<li>The Babel ver.5.8 compiler for JSX syntax is linked online from the Internet;</li>
<li>Project written in Visual Studio Code;</li>
</ul>
<!-- We will put our React component inside this div. -->
<table cellspacing="2" cellpadding="2">
<tr>
<td><div id="React_container1" class="cReact_panel" data-cid="1" data-color="#85e2e2"></div></td>
<td><div id="React_container2" class="cReact_panel" data-cid="2" data-color="#e5e2c2"></div></td>
</tr>
</table>
<div id="React_container3"></div>
<div id="React_container4"></div>
<!-- === SCRIPT =============================================================================== -->
<script type="text/javascript"> //type="text/javascript" type="text/babel"
const FnElement = function(props) { //[Function Component (state less)]
//Hook parameters
const [bShowNameState, setShowNameState] = React.useState(true);
const [iNumberState, setiNumberState] = React.useState(5);
const clickBut = () => {
console.log('Change');
setShowNameState(!bShowNameState);
setiNumberState(iNumberState+1);
}
const getMainElement = (xprops) => {
// ==== Arguments from State =============
const _bShowNameState = bShowNameState;
const _iNumberState = iNumberState;
const _sTitle = 'This is React element [no babel/JSX] from [Function Component] ‘state less’ with Hooks parameter';
/* ====== STYLE ========= */
const jStyleMain = { //Inline CSS Styles
padding: "10px",
margin: "10px",
width: "240px",
borderRadius: "6px",
border: "2px solid #599",
backgroundColor: "#d1c090",
fontFamily: "'Arial','Segoe UI'"
}
/* ====== HTML elements ========= */
const DivStart = React.createElement("div",
{style: { fontSize: '10pt', textAlign: 'left', marginBottom: '6px' }},
_sTitle
);
const newButton = React.createElement(
'button', { onClick: clickBut }, 'Increment me'
);
const text2 = _bShowNameState ? '_ON' :"OFF";
const text3 = ' Count: ' + _iNumberState;
const DivVal = React.createElement("div",
{style: { fontSize: '12pt', fontWeight: 'bold', textAlign: 'left', margin: '6px'}},
'Value is ', text2, text3
);
const newDivMain = React.createElement("div",
{ id: 'IdDivMain', className: 'div_main', style: jStyleMain},
DivStart, newButton, DivVal );
//const aColor1 = xprops.xColor1; // '#a5efe2';
if (typeof xprops.xColor1 !== 'undefined')
newDivMain.props.style.backgroundColor = xprops.xColor1;
return (newDivMain);
}
return(getMainElement(props));
}
class RButton extends React.Component { //[Class Component (state full)]
constructor(props) {
super(props);
this.state = { liked: false };
this.state = { count: 0 };
}
clickBut() {
this.setState({ liked: !this.state.liked });
this.setState({ count: this.state.count + 1 });
}
getMainElement(xprops) {
// ==== Arguments from State =============
const _bShowNameState = this.state.liked;
const _iNumberState = this.state.count;
const _sTitle = 'This is React element [no babel/JSX] from [Class Component - state full] with States parameter';
/* ====== STYLE ========= */
const jStyleMain = { //Inline CSS Styles
padding: "10px",
margin: "10px",
width: "240px",
borderRadius: "6px",
border: "2px solid #599",
backgroundColor: "#d1c090",
fontFamily: "'Arial','Segoe UI'"
}
/* ====== build HTML elements ========= */
const DivStart = React.createElement("div",
{style: { fontSize: '10pt', textAlign: 'left', marginBottom: '6px' }},
_sTitle
);
const newButton = React.createElement(
'button', { onClick: this.clickBut.bind(this) }, 'Change me'
);
const text2 = _bShowNameState ? '_ON' :"OFF";
const text3 = ' Count: ' + _iNumberState;
const DivVal = React.createElement("div",
{style: { fontSize: '12pt', fontWeight: 'bold', textAlign: 'left', margin: '6px'}},
'Value is ', text2, text3
);
const newDivMain = React.createElement("div",
{ id: 'IdDivMain', className: 'div_main', style: jStyleMain},
DivStart, newButton, DivVal );
if (typeof xprops.xColor1 !== 'undefined')
newDivMain.props.style.backgroundColor = xprops.xColor1;
return (newDivMain);
}
render() {
const newDivMain = this.getMainElement(this.props);
return (newDivMain);
}
}
</script>
<!-- ==== BALEL NPX syntax ==================== -->
<script type="text/babel"> //type="text/javascript" type="text/babel"
class RBab2 extends React.Component {
render() {
return (
<div className="BabelBox">
That is a React element [RBab2] from JSX-syntax/babel
</div>
);
}
}
class RBab extends React.Component
{
render() {
console.log('Change');
return (
<div className="BabelBox">
That is a React element [RBab] from JSX-syntax/babel.
</div>
);
}
}
ReactDOM.createRoot(document.querySelector('#React_container3'))
.render(React.createElement(RBab, {}, null));
ReactDOM.createRoot(document.querySelector('#React_container4'))
.render(React.createElement(RBab2, {}, null));
</script>
<!-- ==== GO ==================== -->
<script type="text/javascript">
// For ver React_18. From ver 15 - Error
ReactDOM.createRoot(document.querySelector('#React_container1'))
.render(React.createElement(RButton, {xColor1: '#45e2e2'}, null));
ReactDOM.createRoot(document.querySelector('#React_container2'))
.render(React.createElement(FnElement, {xColor1: '#9592B2'}, null));
</script>
</body>
</html>