Repository navigation
Expand file tree
/
Copy pathreact_notes
More file actions
165 lines (123 loc) · 6.2 KB
/
Copy pathreact_notes
File metadata and controls
165 lines (123 loc) · 6.2 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
Why we use React -> React is a robust advanced language .
In javascript if the user wants to change any data in the web than the js reload(render) the whole website .
So due to this it takes time to load the page. And even if we have to do small changes it takes lots of time .
Therfore facebook introduced REACT which is efficient .
React consist -> 1. React libraries 2. React DOM
if we have to do somethind in the website than we have to give the data to react and react will do the work with the help
of virtual DOM. The DOM only change the required part without touching the correct part . due to which the website run fast.
JSX -> means we can write javascript and html in one file . in javascript if we have to write the code of html then we use to do like this ${} and in this
curly brases we was wrote the code.
In React we have to give command to React and he will do the job
Here we are trying to change the variable name and print it to the page
the changes is done in the console but in the page nothing is visible
Because, if we have to do any work we have to give command to react
const App = () => {
let a = "Aryan";
const ab = () =>{
a = "Aman";
console.log("Hello from {a}");
}
return (
<div>
<h1>Hello from {a}</h1>
<button onClick={ab}>Click</button>
</div>
)}
So to do this we need to use HOOKS
1. useState
Here we did it using useState function
const [num,setNum] = useState(0); setNum -> we give commmand to react to set the var to the following number
return (
<div>
<h3>Number is {num}</h3>
<button onClick={() => setNum(num + 10)}>Increment</button>
<button onClick={() => setNum(num - 10)}>Decrement</button>
</div>
)
CHAP 01 BASICS
DIFFERENCE BETWEEN LIBRARY AND FRAMEWORK
Library means here you can do seperate work using a single library.
A Library GSAP use to make animation
A Library React use to make the UI
Framework means a structure where all parts of the website includes.
Library = you call it. Framework = it calls you.
Example of both -> IF you have to create a house
Then in Library -> you can customize according to you, you have freedom to do it.
In Framework -> it becomes like you are in a flat,where you can do small changes to customize your room.
DIFFERENCE BETWEEN REAL DOM AND VIRTUAL DOM
The Real DOM is the actual webpage structure in the browser. When something changes, the browser updates it directly,
which can be slow because it may re-render large parts of the page.
The Virtual DOM, used by React, is a lightweight copy of the Real DOM. React first updates this copy, compares the changes,
and then updates only the necessary parts in the Real DOM, making it faster and more efficient.
Real DOM updates the whole structure slowly. Virtual DOM updates only the changed part efficiently.
CHAP 02 BASIC PART 02
In a function, we can return only one thing
Here we return hello, we cannot write another thing in this, so to solve this we use div
function app() {
return 'hello';
}
function app() {
return <div> <h1> hello <h1\>
<h2> Welcome <h2\>
<div\>
}
so we can use like this
IF we have to return 2 div in a function,so we will create a div and place both the div that we have to return
function app() {
return <div>
<div> <h1> hello <h1\><div\>
<div> <h1> welcome<h1\><div\>
<div\>
}
FRAGEMENTS
but in the console the extra div is visible and this is not the optimal way so we introduce FRAGEMENTS
<><\> <- This is a fragment so we can use it inplace of div
<>
<div> <h1> hello <h1\><div\>
<div> <h1> welcome<h1\><div\>
<\>
COMPONENTS
in React we create everything in components.Eg:- If i created a website then the navbar is a component the body is a component, the things inside the body will be of different component.
Why:- Because,if we have to make changes in the web,then the whole page will not render , only the part will load which we are changing. It is possible because we create it as a component.
You can check the component file where we create a component and we can use as many time because it is a function.
If we have to print the var using tags in jsx
const Card = () => {
const user = 'aman';
return (
<div>
<h1 className='card'>This Card is made by {user}</h1> here wrote the var in the curly braces
</div>
)}
Now we move to an Impotant topic
PROPS (Prperty)->
why we need props ?
-> if we have to create few components and we are changing the inside content of the components.
So we will be require Props.
Basically Props (properties) are inputs you pass from a parent component to a child component.
we only pass from parent to child.
You can seee the examples in the props folder
04 card project
Very important Mini project- depth understanding of props
we created this using the props where we pass the value of each props using array
we create a array of object and store the values and iterate using arr.map.
we also have to pass a unique id to understand the react so we pass the key of map named idx
05 CSS
Way 01
we can simply write the css of all the components in the index.css file using the className.
Limitation:- Suppose we create a component button
and also create a component header
then we write the css of both using className
-> when we give the same className of button to the button of the header then it occupy The
same css of the button component.
-> If we want to do different css of the button of the header then we cannot do it.
Way 02
Here we create a different css file for different components, where we create a button.css,header.css for the
components (button,header).
But,same error will occur here so we are not prefering this.
You can check the code of the module css.
First we create 2 folders of Header and Button
we create a header.css and button.css.
we wrote the css in both the file.
-> when we try to the className of the button in the button of the header, we cannot able to access it bcz
it was in different module so we can easily style this button using different css. Solved the problem.
Way 03 Tailwind css (Very IMP)