Skip to content

Commit 5adb79b

Browse files
Add tutorial content + update styles
1 parent 13dba15 commit 5adb79b

12 files changed

+101
-22
lines changed

scripts/processMarkdown.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ const MARKDOWN_FILE_EXTENSION = 'md';
88

99
function checkBuildFolderExist(){
1010
if (!fs.existsSync(TUTORIAL_BUILD_PATH)) {
11+
console.log(chalk.magenta(`Creating build directory`));
1112
fs.mkdirSync(TUTORIAL_BUILD_PATH);
1213
}
1314
}
1415

1516
function cleanBuildFolder(){
17+
console.log(chalk.yellow(`Cleaning up build directory`));
1618
fs.readdir(TUTORIAL_BUILD_PATH, function (err, items) {
1719
items.forEach(function(file) {
1820
var filePath = path.join(TUTORIAL_BUILD_PATH, file);
@@ -21,14 +23,14 @@ function cleanBuildFolder(){
2123
console.log(chalk.red(`Error when deleting file %s`), filePath);
2224
throw new Error(err);
2325
}
24-
console.log(chalk.yellow(`Deleted file - %s`), filePath);
2526
})
2627
})
2728
});
2829
}
2930

3031

3132
function createJSFilesFromMarkdown(){
33+
console.log(chalk.green(`Building tutorial files from markdowns`));
3234
fs.readdir(TUTORIAL_PATH, function(err, items) {
3335
items.forEach(function(file) {
3436
var filePath = path.join(TUTORIAL_PATH, file);
@@ -43,8 +45,6 @@ function createJSFilesFromMarkdown(){
4345
console.log(chalk.red(`Error writing file - %s`), newFilePath);
4446
throw new Error(err);
4547
}
46-
console.log(chalk.green(`File sucessfully created - %s`), newFilePath);
47-
4848
})
4949
})
5050
}

src/exercise/solution/01-helloWorld-solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22

33
function HelloWorld(props){
44
return (

src/exercise/solution/02-IntroToJSX-solution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22

33
function HelloWorld(props){
44
return (

src/index.css

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ body {
1111
code {
1212
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
1313
monospace;
14-
border-radius: 8px;
15-
background-color: #e4d3bc;
14+
border-radius: 5px;
15+
background-color: #ccc7c2;
1616
padding: 0px 5px 0px 5px;
1717
}
1818

19-
a {
19+
blockquote {
20+
border-left: solid 5px #9ba1b1;
21+
padding-left: 20px;
22+
font-style: italic;
23+
}
24+
25+
.App a {
2026
color: #3f51b5;
2127
text-decoration: none;
2228
}
@@ -34,7 +40,9 @@ button {
3440
min-height: 100vh;
3541
/* display: flex; */
3642
/* flex-direction: column; */
37-
font-size: 1.1rem;
43+
font-size: 1rem;
44+
font-weight: 400;
45+
line-height: 24px;
3846
color: black;
3947
}
4048

src/setup/AppShell.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Link } from 'react-router-dom';
1919

2020
import { TutorialMetadataApi } from './TutorialMetadataApi';
2121

22-
const drawerWidth = 240;
22+
const drawerWidth = 260;
2323

2424
const styles = theme => ({
2525
root: {
@@ -46,6 +46,11 @@ const styles = theme => ({
4646
hide: {
4747
display: 'none',
4848
},
49+
leftMenuNav: {
50+
color: '#0052CC',
51+
textDecoration: 'none',
52+
fontWeight: '400'
53+
},
4954
drawer: {
5055
width: drawerWidth,
5156
flexShrink: 0,
@@ -140,7 +145,7 @@ class PersistentDrawerLeft extends React.Component {
140145
{TutorialMetadataApi.getTutorialMetadata().map((tutorial, index) => (
141146
<ListItem button key={tutorial.displayName}>
142147
<ListItemText>
143-
<Link onClick={this.handleDrawerClose} to={tutorial.route}>{tutorial.displayName}</Link>
148+
<Link className={classes.leftMenuNav} onClick={this.handleDrawerClose} to={tutorial.route}>{tutorial.displayName}</Link>
144149
</ListItemText>
145150
</ListItem>
146151
))}

src/setup/Exercise.jsx

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const SolutionTitle = styled.div`
1818
`;
1919

2020
class Exercise extends Component {
21-
constructor(props) {
22-
super(props);
23-
}
24-
2521
render() {
2622
const { exercise: { location, solutionLocation } } = this.props;
2723
return (

src/setup/Markdown.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Markdown extends Component {
4343
source={s}
4444
renderers={{
4545
code: codeRenderer,
46+
link: linkRenderer
4647
}}
4748
/>
4849
);

src/setup/ScrollToTop.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { withRouter } from 'react-router';
33

4-
class ScrollToTop extends Component {
4+
class ScrollToTop extends React.Component {
55
componentDidUpdate(prevProps) {
66
if (this.props.location !== prevProps.location) {
77
window.scrollTo(0, 0);

src/setup/renderer/CodeBlock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Lowlight from 'react-lowlight';
33
import js from 'highlight.js/lib/languages/javascript';
44
import xml from 'highlight.js/lib/languages/xml';
55
import 'highlight.js/styles/default.css';
6-
import 'highlight.js/styles/vs2015.css';
6+
import 'highlight.js/styles/dracula.css';
77

88
Lowlight.registerLanguage('js', js);
99
Lowlight.registerLanguage('xml', xml);

src/tutorial/01-HelloWorld.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Our `HelloWorld` component will look like this:
22

33
```jsx
4+
import React from 'react';
5+
46
function HelloWorld(props){
57
//function must return something
68
return(
@@ -10,7 +12,7 @@ function HelloWorld(props){
1012
```
1113
Now the question is what do we return from this function.
1214

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.
1416

1517
In our case let's say we want to render a `div` element that has `Hello World` text like `<div>Hello World</div>`
1618

@@ -24,10 +26,75 @@ You should try this for yourself. Open the exercise file and edit the function t
2426

2527
<!--exercise-->
2628

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:
2830

2931
```html
3032
<div>
3133
Hello World
3234
</div>
3335
```
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+
<div id="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+
const helloWorldDiv = document.createElement('div');
64+
helloWorldDiv.append('Hello World');
65+
66+
//Select the root node and append the div created above
67+
const root = 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.
81+
82+
```jsx
83+
ReactDOM.render(HelloWorld, document.getElementById('root'))
84+
```
85+
86+
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.

src/tutorial/02-IntroToJSX.md

+1-1
Original file line numberDiff line numberDiff 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:
22

33
```jsx
44
return (

src/tutorial/06-LifecycleMethods.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ class CompanyProfile extends React.Component {
8080

8181
```
8282

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
84+
[React documentations](https://reactjs.org/docs/react-component.html#the-component-lifecycle)
85+
for extensive list of all the lifecycle methods and also to understand when you should and shouldn't use those lifecycle methods.

0 commit comments

Comments
 (0)