-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_react.html
More file actions
189 lines (135 loc) · 3.73 KB
/
javascript_react.html
File metadata and controls
189 lines (135 loc) · 3.73 KB
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
<html>
<head>
</head>
<body>
<div id="root"></div>
<div id="counterButton"></div>
<div id="multiRender"></div>
<div id="app"></div>
<br />
<div id="countApp"></div>
<br />
<div id="rerenderReact"></div>
<div id="rerenderNonReact"></div>
<br />
<div id="propsTest"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
<script type="text/babel">
function Hello() {
return <div>Hello!</div>; // jsx - React.createElement() call
}
ReactDOM.render(
<Hello /> // component to render
,document.getElementById('root') // where to render
);
function CounterButton() {
const [counter, setCounter] = React.useState(0); // hook
return <button onClick={() => setCounter(counter + 1)}>{counter}</button>; // arrow function
}
ReactDOM.render(
<CounterButton />
,document.getElementById('counterButton')
);
ReactDOM.render(
<React.Fragment>
<Hello />
<CounterButton />
</React.Fragment>
,document.getElementById('multiRender') // render multiple components in single render
);
function App() { // put all components to render in this function
return (
<React.Fragment>
<Hello />
<CounterButton />
</React.Fragment>
)
}
ReactDOM.render(
<App />
,document.getElementById('app') // render function that returns all components to render
);
function CounterButtonDisplay(props) {
return (
<div>
<button onClick={props.click1}> + 1</button>
<button onClick={props.click10}> + 10</button>
</div>
);
}
function Display(props) {
return (
<div>{props.number}</div> // display "number" prop
);
}
function CountApp() {
const [counter, setCounter] = React.useState(1337); // counter displayed by "number" prop
const plusOne = () => setCounter(counter + 1); // called by "click1" prop
const plusTen = () => setCounter(counter + 10); // called by "click10" prop
return (
<div>
<CounterButtonDisplay click1={plusOne} click10={plusTen} />
<Display number={counter} />
</div>
);
}
ReactDOM.render(
<CountApp />
,document.getElementById('countApp')
);
function RenderDate() {
return new Date().toLocaleTimeString();
}
function RenderInput() {
return <input type="text" placeholder="react only re-rendering date" />
}
const rerender = () => {
ReactDOM.render(
<div>
<RenderDate />
<br />
<RenderInput />
</div>
,document.getElementById('rerenderReact')
)
document.getElementById('rerenderNonReact').innerHTML = "<div><input placeholder='non-react re-rendering all' /></div>";
}
setInterval(rerender, 1000);
function PropsTestFuncDisplay(props) {
return (
<div>
{props.blah1}
<br />
{props.blah2}
<br />
{props.blah3}
<br />
{props.displayDate}
<br />
{props.constValue}
</div>
);
}
function PropsTestFunc() {
const someString = "lardydah";
return (
<div>
<PropsTestFuncDisplay
blah1={"prop1"}
blah2={"prop2"}
blah3={"prop3"}
displayDate={Date()}
constValue={someString}
/>
</div>
);
}
ReactDOM.render(
<PropsTestFunc />
,document.getElementById('propsTest')
);
</script>
</body>
</html>