-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReactJS-learning-Brad.txt
More file actions
368 lines (234 loc) · 8.71 KB
/
Copy pathReactJS-learning-Brad.txt
File metadata and controls
368 lines (234 loc) · 8.71 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
* ReactJS is client side Javascript library
* Created & Maintained by Facebook.
* Used to build dynamic user interfaes.
* Component based library.
* React is "technically" a library because it doesn't include a 'router' OR 'Http' like Angular does and so on but it is often refered to as a Framework as it is used for many of the same reasons as a framework.
*
* Components can have "props" & "state"
* Class components can have "state" but "Functional components" could not. But "Hooks" allow us to have "State" within "Functional components".
* Class component example
import React, { Component, Fragment } from "react";
export default class App extends Component {
render(){
return (
<React.Fragment>
<p> Hello World </p>
</React.Fragment>
);
}
}
* Functional component example
import React, { Fragment } from "react";
export default App() {
return (
<React.Fragment>
<p> Hello World </p>
</React.Fragment>
);
}
* We should use "className" in place of "class" with HTML tags. And in "label" tag we should use "htmlFor" in place of "for".
* We should use "this." whenver we call a function which is outside of "render" method.
export default class App extends Component {
render(){
return (
<React.Fragment>
<p> Hello World </p>
{ this.foo() }
</React.Fragment>
);
foo(){
return "Hello";
}
}
}
But if the function is inside of "render" method then we no need to use "this." while calling the function in JSX.
* We can create "defaultProps" as below
-> static defaultProps = {
title: "Github Finder",
icon: "fab fa-github"
}
* In addition to "defaultProps" we also have "PropTypes". In order to use "PropTypes" we need to import it as below.
-> import PropTypes from "prop-types";
-> static propTypes = {
title: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired
}
------------------------------------------------------------------------------------------
* We should create "defaultProps" & "defaultProps" in "class components" as below
import React, { Component } from "react";
import PropTypes from "prop-types";
export default UserItem extends Component {
const { login, avatarUrl } = this.props.user;
static defaultProps = {
login: "Github Finder",
avatarUrl: "https://www.google.com/"
}
static propTypes = {
login: PropTypes.string.isRequired,
avatarUrl: PropTypes.string.isRequired
}
render(){
return (
<React.Fragment>
<p> Hello World </p>
Name is = { login }
Avatar URL is = { avatarUrl }
</React.Fragment>
);
}
}
* We should create "defaultProps" & "defaultProps" in "functional components" as below
import React from "react";
import PropTypes from "prop-types";
export const UserItem = props => (
const { login, avatarUrl } = props.user;
return (
<React.Fragment>
<p> Hello World </p>
Name is = { login }
Avatar URL is = { avatarUrl }
</React.Fragment>
);
);
UserItem.defaultProps = {
login: "Github Finder",
avatarUrl: "https://www.google.com/"
}
UserItem.propTypes = {
login: PropTypes.string.isRequired,
avatarUrl: PropTypes.string.isRequired
}
Note: We can destructure the "props" variables as below as well
export default const UserItem = ({ user: { login, avatarUrl }})
------------------------------------------------------------------------------------------
*
============================================== Errors =============================================
* We will get "parse" error when we don't wrap all the tags in a single tag while returning.
*
===================================================================================================
React JS topics:
---------------------------------------------------------------------------------------------------
* Class based components
* Stateless functional components
* Context API
* React Redux
* React Hooks
* Lifecycle
* PropTypes
* defaultProps
*
**************************************** V2 *****************************************************
* JSX elements are wrapped in an enclosing tag.
*
'Class component' hooks
--------------------------------------
* state
* componentDidMount
* render
* componentDidUpdate
* componentWillUnmount
'Functional component' hooks
--------------------------------------
* useState
* useEffect --> It is an arrow functions without paramaeters
useEffect(() => {
// eslint-disable-next-line
}, []);
* useReducer
* useContext
* In 'Class component' we use 'state' whereas in 'Functional component' we use 'useState' hook
* In 'Class component' we use 'componentDidMount' whereas in 'Functional component' we use 'useEffect' hook
* In 'Class component', 'propTypes' will be diclared inside of the class whereas in 'Functional component' 'propTypes' will be diclared outside of the function with function name.
User.propTypes = {}
* In 'Functional component' there will be no 'render' method & props will be passed as arguments
const User = ({name, id}) => {}
*
*********************************** Imports & shortcuts************************************
* imrc
* imr
* cc
* fc
* import React, { Component, createContext } from 'react';
* import { BrowserRouter, Route, Link, NavLink, withRouter } from 'react-router-dom';
export const ThemeContext = createContext();
*******************************************************************************************
******************************** The NetNinja *********************************************
* 'npx' comes with npm 5.2+ versions.
* Context API - Clean & easy way to share state between components
* 'Context API' is alternative to 'Redux' we can say
* 'contextType' will be used in 'Class component' but not in 'Functional component'.
* 'useEffect()' will be triggered everytime data changes/renders & We can use 'useEffect()' method as many times as want.
useEffect(() => {
console.log('Songs changed');
}, [songs]);
useEffect(() => {
console.log('Age changed');
}, [age]);
* Use 'pascalcase' while creating 'context' files. Ex: BookContext.js
* Use 'camelcase' while creating 'reducer' files. Ex: bookReducer.js
* We have two types of components
-> Container components - Sometimes called as 'class based components'
* Contains state
* Contains life-cycle hooks
* Not concerned with UI
* Use classes to create
-> UI components - Sometimes called as 'stateless'/'Functional components'/'UI' components
* Don't contain state
* Receive data from 'props'
* Only concerned with UI
* Use functions to create
* <button onClick={() => {deleteNinja(ninja.id)}}> Delete Ninja </button>
Redux
**************************************************************************************
* First React creats a 'Virtual DOM' and renders in to the DOM. And React keep watches the changes and if any mondification occures, then React compares that change with 'Virtual DOM' and then reders that particular change to the DOM.
* A layer on-top of React
* Components look like HTML template (actually JSX)
* They can contain 'state' (data or UI state)
* They also can contain javascript for functionality
* Only 'Class based components' contain 'Life-cycle' hooks but not 'Functional components'
* 'axios' is a HTTP request library.
*
Routing Example:
******************************************************************************************
import React, { Component } from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contact';
class App extends Component {
render(){
return (
<BrowserRouter>
<div className='App'>
<Navbar />
<Route exact path='/' component={Home} />
<Route exact path='/about' component={About} />
<Route exact path='/contact' component={Contact} />
</div>
</BrowserRouter>
);
}
}
export default App;
-------------------------------------------------------------
import { Link, NavLink, withRouter } from 'react-router-dom';
const Navbar = (props) => {
console.log(props);
setTimeout(() => {
props.history.push('/about');
}, 2000);
return (
<ul>
<li><Link to='/'>Home</Link>
<li><Link to='/about'>About</Link>
<li><Link to='/contact'>Contact</Link>
</ul>
);
}
export default withRouter(Navbar);
* If we use '<NavLink>' instead of '<Link>' tag, '<NavLink>' adds a class named 'active'.
* React router automatically applies some properties(Ex; history) to the 'props' by default on every route.
* But we can add those default props with 'Higher order component' which is 'withRouter'
******************************************************************************************
-------------- React, Redux & Firebase App Tutorial - The Net Ninja ----------------------
*