-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathApp.js
173 lines (149 loc) Β· 5.67 KB
/
App.js
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
import React from "react";
import ReactDOM from "react-dom/client";
// create heading element using React.createElement
// React.createElement => ReactElement (JS Object) => HTMLElement (render) => DOM (Browser)
/*
const heading = React.createElement("h1", { id: "heading" }, "Namasate React π");
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(heading);
*/
// create heading element using JSX
// JSX => React.createElement => ReactElement(Object) => HTMLElement(render) => DOM(Browser)
/*
const jsxHeading = <h1 id="heading">Namasate React using JSX π</h1>;
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(jsxHeading);
*/
/**
* React Component
* Functional component - new way of writing component
* Class component - old way of writing component
* React component is a function or class that returns a React Element (JS Object)
*/
// React Functional Component
const elem = "React Element";
// const Title = () => {
// return <h1 className="head" id="heading"><h1>{elem}</h1> Namaste React using JSX π</h1>
// };
const Title = () => (
<h1 className="head" id="heading">
<h1 style={{color: "red"}}>{elem}</h1>
Namaste React using JSX π
</h1>
);
// React Fragment: Behaves like an empty tag
// Component Composition - multiple components can be composed together to create a new component (Component Composition)
const HeadingComponent = () => (
<>
<div id="container">
{/* {Title()} */}
{/* <Title></Title> */}
<Title />
<h1 className="heaidng">Namaste React Fucntional Component</h1>
</div>
<div id="container-2"></div>
</>
);
const root = ReactDOM.createRoot(document.getElementById("root"));
// root.render(Title());
// root.render(<Title />);
root.render(<HeadingComponent />);
// root.render(HeadingComponent());
/**
* React.createElement: Function (createElement) => ReactElement (JS Object) => HTMLElement (render) => DOM (Browser)
*
* React.createElement(type, props, children)
* type: HTML tag name or React Component
* props: Object, null or empty
* children: String, React Element, Array of React Elements
*
* React Element: Object
* React Element is a plain JavaScript object that represents a DOM element or a component.
*
* ReactDOM.createRoot(containerElement)
* containerElement: HTMLElement
*
* ReactDOM.createRoot(containerElement) => root
* root.render(ReactElement)
*
* React Element => Object => HTMLElement(render)
*/
/*
const heading = React.createElement("h1", {id: "heading"}, "Namasate React π");
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(heading);
*/
/**
* JSX: JavaScript XML
* JSX => React.createElement => ReactElement(JS Object) => HTMLElement(render) => DOM(Browser)
*
* JSX is a syntactic sugar for React.createElement
* JSX is not a valid JavaScript, it needs to be transpiled using Bable to React.createElement before it can be rendered in the browser using ReactDOM
* JSX (transpiled before it can be rendered in the browser using ReactDOM) => Parcel (Bundler) => Babel (Transpiler) => React.createElement => ReactDOM
* JSX is optional, you can write React without JSX
* JSX is more readable and easy to write
* JSX is more like HTML
* JSX is look like HTML or XML like syntax, but it's not HTML or XML. It's a syntax extension of JavaScript.
* JSX is mixed of JavaScript and HTML like syntax that allows us to write HTML like code in React.
* JSX prevents cross-site injection attacks by escaping any values embedded in JSX before rendering them.
*
* Babel is a transpiler that converts JSX into React.createElement calls before it can be rendered in the browser using ReactDOM
* Babel: JavaScript compiler that takes your modern JavaScript code (ES6) and returns code that most browsers can understand.
*/
/*
const jsxHeading = <h1 id="heading">Namasate React using JSX π</h1>;
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(jsxHeading);
*/
/**
* React Component
* Functional component - new way of writing component
* Class component - old way of writing component
* React component is a function or class that returns a React Element (JS Object)
* React component is a reusable piece of UI
* React component is a building block of React application
* React component is a pure JavaScript function or class
* React component is a function that accepts props and returns a React Element
* React component is a function that accepts props and returns JSX
* React component is a function that accepts props and returns React.createElement
*/
/*
const Title = () => (
<h1 className="head" id="heading">
Namaste React using Functional Component π
</h1>
);
const root = ReactDOM.createRoot(document.getElementById("root"));
// root.render(Title());
root.render(<Title />);
*/
/**
* React Fragment: Behaves like an empty tag
* React Fragment is a component that allows you to group multiple children without adding extra nodes to the DOM
* React Fragment is a component that allows you to return multiple children without adding extra nodes to the DOM
*
* Syntax:
* <React.Fragment>...</React.Fragment>
* or
* <>...</>
*/
/*
const elem = "React Element";
const Title = () => (
<h1 className="head" id="heading">
<h1 style={{color: "red"}}>{elem}</h1>
Namaste React using JSX π
</h1>
);
// React Fragment: Behaves like an empty tag
// Component Composition - multiple components can be composed together to create a new component (Component Composition)
const HeadingComponent = () => (
<>
<div id="container">
<Title />
<h1 className="heaidng">Namaste React Fucntional Component</h1>
</div>
<div id="container-2"></div>
</>
);
*/