You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/tutorial/01-HelloWorld.md
+69-2
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
Our `HelloWorld` component will look like this:
2
2
3
3
```jsx
4
+
importReactfrom'react';
5
+
4
6
functionHelloWorld(props){
5
7
//function must return something
6
8
return(
@@ -10,7 +12,7 @@ function HelloWorld(props){
10
12
```
11
13
Now the question is what do we return from this function.
12
14
13
-
React renders on the broswer what you return from this function component. So basically return is telling React what the DOM should look like when rendering this component on the browser. In case if you're using `React.Component` approach (instead of `function` approach like above), it's what you return from `render` function that tells React what the DOM should look like when the component is rendered.
15
+
The return of this function is telling React what the DOM should look like when this component is rendered on the browser. In case if you're using `React.Component` approach (instead of `function` approach like above), it's what you return from `render` function that tells React what the DOM should look like when the component is rendered.
14
16
15
17
In our case let's say we want to render a `div` element that has `Hello World` text like `<div>Hello World</div>`
16
18
@@ -24,10 +26,75 @@ You should try this for yourself. Open the exercise file and edit the function t
24
26
25
27
<!--exercise-->
26
28
27
-
Notice that `React.createElement` is a simple javascript function which takes three arguments. First argument is the element you want to render. In our case its a `div` element. Second argument is any properties we want to pass to that element. In our case we are not passing anything so it's null. Third argument is the children for this component. In this case it's the text we want to display - `Hello World`. So with this we are telling React to render a div element on the DOM like this:
29
+
Notice that `React.createElement` is a simple javascript function which takes three arguments. First argument is the element you want to render. In our case its a `div` element. Second argument is any properties we want to pass to that element. In our case we are not passing anything so it's null. Third argument is the children for this component. In this case it's the text we want to display - `Hello World`. So with this we are telling React to render a div element like this:
28
30
29
31
```html
30
32
<div>
31
33
Hello World
32
34
</div>
33
35
```
36
+
37
+
Congratulations, you have created your first Hello World React component.
38
+
39
+
Now your inquisitive mind is probably asking - how in the world React renders this thing on the browser?
40
+
41
+
## Rendering
42
+
43
+
Let's step back from React for a moment and think about how we can create the similar Hello World `div` using pure Javascript. Yes pure Javascript - without any frameworks.
44
+
45
+
### Good Ol' Days
46
+
47
+
Let's imagine you have a barebone `html` file that looks like below. It has a `div` with id `app` inside `body`. Pretty simple.
48
+
49
+
```html
50
+
<html>
51
+
<head></head>
52
+
<body>
53
+
<divid="root"></div>
54
+
</body>
55
+
</html>
56
+
```
57
+
58
+
Now imagine inside the `div` with id `root` we want to render another `div` that says `Hello World`. The only catch is we want to do that programmatically using pure Javascript.
59
+
To achieve this we can probably do something like this:
60
+
61
+
```js
62
+
//Create a div node and append Hello World text
63
+
consthelloWorldDiv=document.createElement('div');
64
+
helloWorldDiv.append('Hello World');
65
+
66
+
//Select the root node and append the div created above
67
+
constroot=document.getElementById('root');
68
+
root.appendChild(helloWorldDiv);
69
+
```
70
+
71
+
Here we are creating a `div` node with `Hello World` text and appending that `div` as a child of root `div`.
72
+
73
+
We can actually write our entire application this way - creating elements, removing elements, appending elements etc ourselves. As a matter of fact we did write applications this way before all these UI frameworks/libraries started to mushroom.
74
+
75
+
### Age of React
76
+
Simple example like above are not that hard to write with pure Javascript but once your application gets bigger, it gets messier. That's where libraries like React come to rescue - they hide away from us the messier part of rendering on the browser.
77
+
78
+
Core React library itself doesn't really know how to render anything on the browser. The reason being core React library is designed to work on browser as well as native applications. Thus the job of rendering your component on the browser is done by another library provided by React team called `ReactDOM`.
79
+
80
+
Now let's get back to the `HelloWorld` React component we created at the top of this page and see how we can use ReactDOM to render that component to the browser.
Here we are calling a function called `render` on `ReactDOM` object. The first argument of the function is the component you want to render - in our case `HelloWorld`. Second argument is a document selector. ReactDOM appends the component we want to display (first argument) as a child of the node returned by the selector (second argument).
87
+
88
+
Compare this solution to the pure Javascript solution we looked at earlier. With pure Javascript we were doing the DOM manipulation ourselves - creating the `div`, appending the text and appending the newly created `div` to the `div` with id `root` as it's child. But with React we are not doing any DOM manipulation ourselves. Basically we are saying to React -
89
+
90
+
> Hey React I have a component I want to render. I will tell you what the component should look like when it's rendered (remember this is what the return of the Component function tells). I will also tell you where to render this component (second argument we passed to `ReactDOM.render` function). But I don't want to get involved in DOM manipulation - I will let you do all the DOM manipulation yourself. You can call all these DOM api like `document.createElement`, `.append`, `.appendChild` etc. whenever you wish - I trust you and I don't care as long as I see on the browser what I expected to see.
91
+
92
+
Ok then how does React do it internally? We won't go into all the details of it but briefly talk about one important part of React implementation called Virtual DOM.
93
+
94
+
### Virtual DOM
95
+
[DOM (Document Object Model)](https://www.w3schools.com/js/js_htmldom.asp) is an object representation of the HTML. To see what it looks like open chrome dev tools (Right Click + Inspect) and type `console.dir(document)` and hit enter, you will see a JSON-like tree structure with fields and methods. React for it's part maintains a copy of this DOM - what's called a [virtual DOM](https://reactjs.org/docs/faq-internals.html), named so because it's not a real one, it's a virtual copy.
96
+
97
+
Why does React hold a copy of the DOM? The main reason it maintains a virtual copy of the DOM is to improve performance of the application.
98
+
Web applications these days are very complex. User interacts with the app or the app fetches data and based on that the DOM is updated so that users sees the effects of their interaction or new data. This updating of DOM, however, is an expensive operation - creating and removing DOM nodes (like we did with `document.createElement('div')` above) are expensive. So React optimizes this updating operations using virtual DOM.
99
+
100
+
The way this roughly works is: when there's anything that will cause a UI to update (called re-render), React first updates it's virtual DOM instead of real DOM. Then it compares the virtual DOMs (before and after the update). It has heuristic algorithm to determine which piece of the DOM might have changed. Once it figures that out, it updates only the changed piece on the real DOM. Again the reason it does this is because updating the DOM is expensive operation and it wants to optimize this piece so it only updates what is absolutely necessary instead of tearing down the entire DOM and recreating it.
Copy file name to clipboardexpand all lines: src/tutorial/02-IntroToJSX.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
Another alternative way of telling React what to render is by using JSX. JSX is a very common and recommended way (preferred than `React.createElement` syntax in most cases) to write React code. JSX is a funny looking syntax though - its not purely HTML, its not purely Javascript. But its extension of Javascript where you can write HTML like syntax with full power of Javascript. For example, the equivalent of return statement we saw in [previous page](/tutorial/hello-world) (using `React.createElement`) in JSX would be:
1
+
In the [previous section](/tutorial/hello-world) we created our first Hello World function component. Remember we returned `React.createElement` from the function to tell React what the DOM should look like when we render this component. Another alternative way of telling React what the DOM should look like is by using JSX. JSX is a very common and recommended way (preferred than `React.createElement` syntax in most cases) to write React code. JSX is a funny looking syntax though - its not purely HTML, its not purely Javascript. But its extension of Javascript where you can write HTML like syntax with full power of Javascript. For example, the equivalent of return statement we saw in [previous page](/tutorial/hello-world) (using `React.createElement`) in JSX would be:
Copy file name to clipboardexpand all lines: src/tutorial/06-LifecycleMethods.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -80,4 +80,6 @@ class CompanyProfile extends React.Component {
80
80
81
81
```
82
82
83
-
These are just some common use cases when you want to use lifecycle methods provided by react. Please refer to the [React documentations](https://reactjs.org/docs/react-component.html#the-component-lifecycle) for extensive list of all the lifecycle methods and also to understand when you should and shouldn't use those lifecycle methods.
83
+
These are just some common use cases when you want to use lifecycle methods provided by react. Please refer to the
0 commit comments