Sample-eBook - Version 2
Sample-eBook - Version 2
HTML
TO
REACT
The Ultimate Guide
NGNINJA
ACADEMY
NgNinja Academy
Table Of Content
Leaf
Module 3 - Display and position your elements
Box model
Total width of an element should be calculated like this:
Total height of an element should be calculated like this:
Margins
Margin On Individual Sides
Margin Shorthands
Auto Margin
Paddings
Padding On Individual Sides
Padding Shorthands
Display
Block
Inline
Inline-block
None
Visibility Hidden
Flex
Positions
Static
Relative
Absolute
Fixed
Centering:
Centering Vertically
CSS Float
Clearing Floats
Methods to clear float:
Module 4 - Semantic HTML5
Semantic HTML?
More about Semantic HTML
Difference between HTML and HTML5?
HTML
HTML 5
Below are new semantic elements
What elements have disappeared in the latest HTML?
Difference between <div> and <frame>?
What is HTML5 Web Storage?
localStorage:
sessionStorage:
Module 5 - Flexbox intro and media query
2 / 10
NgNinja Academy
Flexbox
Media queries
Always Design for Mobile First
Orientation: Portrait / Landscape
Let's talk about the sizes - px vs em vs rem
How to calculate PX from REM
More on rem vs em
CSS Grids
Flexbox
Grids
Nested grids
Start-End points of Grid
Module 6 - Quirks, tips, and tricks
FOUC
How to avoid it?
BEM naming convention
OOCSS - Object Oriented CSS
CSS User Agent Styles
Normalizing CSS
Reset CSS
Validate your CSS
Testing Strategies
Conditional CSS
JavaScript
Module 1 - JavaScript Basics
Who created JS?
What is javascript
Why do you love JavaScript?
Your first "hello world" program
Run just JavaScript
Variables
Data Types in JavaScript
Basic Operators
Special Operators
Fun with Operators
JavaScript as Object-Oriented Programming language
Polymorphism Example in JavaScript
3 / 10
NgNinja Academy
Example of Prototype
What is Prototypal Inheritance?
Understand Prototypal Inheritance by an analogy
Why is Prototypal Inheritance better?
Example of Prototypal Inheritance
Linking the prototypes
Prototype Chain
How does prototypal inheritance/prototype chain work in above example?
Module 5 - Advanced JavaScript (Closures, Method Chaining, etc.)
Hoisting in JavaScript
Another example
We get an error with Function Expressions
JavaScript Closures
Closure remembers the environment
IIFE
What is happening here?
Closure And IIFE
JavaScript call() & apply() vs bind()?
bind
Example using bind()
call()
apply
ReactJS
Module 1 - Getting started
What is react?
Features
Why react / Benefits
Other Benefits
Limitations
What Are SPAs
SPA
Pros
Cons
Installing React
Prerequisites
Install Node
Install Yarn
Install ReactJS using create-react-app
Online Playgrounds
Deploying React App To Internet
Deploy to Netlify
Easy setup deploy
Module 2 - Basics of React
React JSX
JSX - confusing parts
Virtual DOM
Cons of real DOM
Enter -> Virtual DOM
Diffing
React Components
Thinking in components
Component Render
Function Components
Class Components
NOTE: Please prefer using Function components with React hooks whenever you need to
create component that need to track it's internal state.
Pure components
Reusing Components
States And Props
States
6 / 10
NgNinja Academy
Props
Event Handling
Bind this
Passing Arguments
Two Way Binding
One way data binding
Two Way - 2 way binding
Module 3 - Styling your components
Inline Styles
CSS Stylesheets
Dynamic Styles
Module 4 - Advanced React
Conditional Rendering
Outputting Lists
Keys
Higher Order Components
Cons of HOC
Render Props
Con
Component Lifecycle
initialization
mounting
componentWillMount()
componentDidMount()
static getDerivedStateFromProps()
updating
componentWillReceiveProps()
shouldComponentUpdate()
getSnapshotBeforeUpdate()
componentWillUpdate()
componentDidUpdate()
unmount
Error handling
componentDidCatch()
Web Development
Module 1 - Web Development Tooling
Git and GitHub Basics
What is Git
Install Git
Create Git project
Adding a file to your repo
Committing your file
Git clone
How to clone Git repo?
Git Branching
Checkout the branch
Merging branches
Git status
Webpack
Installing Webpack
Webpack in-depth
Webpack config
Module 2 - HTTP and API
What is HTTP
8 / 10
NgNinja Academy
10 / 10
NgNinja Academy
1 / 66
NgNinja Academy
<!DOCTYPE html>
2 / 66
NgNinja Academy
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<h1>My First Web Page</h1>
</body>
</html>
3 / 66
NgNinja Academy
DOM
Document object model
NOTE: Modern browsers are smart. If the browser encounters malformed HTML, it automatically
corrects it when making the DOM. For ex: browser will auto insert <html> tag at the top is not
provided.
4 / 66
NgNinja Academy
5 / 66
NgNinja Academy
6 / 66
NgNinja Academy
Semantic HTML?
It is a coding style
Semantic elements == elements with a meaning
Good for SEO
Good for accessibility
Especially for visually impaired people
Which rely on browser speech, screen readers to interpret page content clearly
8 / 66
NgNinja Academy
9 / 66
NgNinja Academy
Media queries
It was introduced in CSS3
It uses @media rule to include CSS only if a certain condition is true
Media queries allow you to target any platform you desire and write specific styles for that platform
This is how you write responsive styles
Different styles for desktops vs tablets vs mobiles
Simple example below
// If the browser window is smaller than 500px, the background color will
change to lightblue:
/* Landscape tablets */
@media only screen and (min-width: 768px) {
/* Laptops/desktops */
@media only screen and (min-width: 992px) {
11 / 66
NgNinja Academy
12 / 66
NgNinja Academy
13 / 66
NgNinja Academy
JavaScript
14 / 66
NgNinja Academy
15 / 66
NgNinja Academy
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<script>
console.log("Hello World");
</script>
</body>
</html>
JavaScript code is written in between the script tag in the above code.
When the page loads the browser will run the code between the script tag.
alert() function will be called which will create a model with hello world text on it.
Variables
Variables are containers
They store data values
For ex: var x = 5
5 is the value stored in variable x
In programming, just like in mathematics, we use variables to hold values
16 / 66
NgNinja Academy
// More examples
17 / 66
NgNinja Academy
18 / 66
NgNinja Academy
Scoping in JavaScript
Every variable defined in JavaScript has a scope
Scope determines whether the variable is accessible at a certain point or not
Two Types
Local scope
Available locally to a "block" of code
Global scope
Available globally everywhere
JavaScript traditionally always had function scope. JavaScript recently added block scope as a
part of the new standard. You will learn about this in the Advanced JavaScript module.
Examples
Function parameters are locally scoped variables
Variables declared inside the functions are local to those functions
// global scope
var a = 1;
function one() {
console.log(a); // 1
}
one(); // 1
two(2); // 2
19 / 66
NgNinja Academy
three(); // 3
var a = 1
function four(){
if(true){
var a = 4
}
20 / 66
NgNinja Academy
Interview Question: What is the difference between the new operator and
Object.create Operator
new Operator in JavaScript
This is used to create an object from a constructor function
The new keywords also execute the constructor function
function Car() {
console.log(this) // this points to myCar
this.name = "Honda";
}
Object.create in JavaScript
You can also use Object.create to create a new object
But, it does not execute the constructor function
Object.create is used to create an object from another object
const Car = {
name: "Honda"
}
21 / 66
NgNinja Academy
22 / 66
NgNinja Academy
JavaScript Classes
Classes were introduced in ES6 standard
Simple Person class in JavaScript
You can define constructor inside the class where you can instantiate the class members
Constructor method is called each time the class object is initialized
class Person {
constructor(name) {
this.name = name
}
}
Class methods
You can add your functions inside classes
These methods have to be invoked programmatically in your code
class Person {
constructor(name) {
this.name = name
}
getName() {
return this.name
}
}
john.getName() // John
JavaScript class is just syntactic sugar for constructor functions and prototypes
If you use typeof operator on a class it logs it as "function"
23 / 66
NgNinja Academy
example:
class Foo {}
console.log(typeof Foo); // "function"
Man.prototype = Object.create(Person.prototype)
Man.prototype.constructor = Man
console.log(John.name) // John
console.log(John.gender) // Male
class Person {
constructor(name){
this.name = name
}
}
console.log(John.name) // John
console.log(John.gender) // Male
25 / 66
NgNinja Academy
26 / 66
NgNinja Academy
ReactJS
27 / 66
NgNinja Academy
What is react?
It is a UI library developed at Facebook
Create interactive, stateful, and reusable components
Example:
Instagram.com is written in React completely
Uses virtual DOM
In short: React selectively renders subtree of DOM based on state changes
Server side rendering is available
Because fake/virtual DOM can be rendered on server
Makes use of Isomorphic JS
Same JS code can run on servers and clients...
Other egs which does this- Rendr, Meteor & Derby
It is the V in MVC
Features
Quick, responsive apps
Uses virtual dom
Does server side rendering
One way data binding / Single-Way data flow
Open source
28 / 66
NgNinja Academy
29 / 66
NgNinja Academy
30 / 66
NgNinja Academy
React JSX
It is JavascriptXML
It is used for templating
Basically to write HTML in React
It lets you write HTML-ish tags in your javascript
It's an extension to ECMAScript
Which looks like XML
You can also use plain JS with React
You don't HAVE to use JSX
But JSX is recommended
JSX makes code more readable and maintainable
Ultimately Reacts transforms JSX to JS
Performs optimization
JXK is type safe
so errors are caught at compilation phase
// With JSX
const myelement = <h1>First JSX element!</h1>;
ReactDOM.render(myelement, document.getElementById('root'));
// Without JSX
ReactDOM.render(myelement, document.getElementById('root'));
whitespaces
React removes spaces by default
You specifically give it using {' '}...
For adding margin padding
// JSX
const body = (
<body>
<span>Hello</span>
<span>World</span>
</body>
)
Children props
They are special kind of props
You will learn about props more in the following sections
Whatever we put between tags is children
Received as props.children
<User age={56}>Brad</User>
// Same as
<User age={56} children="Brad" />
32 / 66
NgNinja Academy
Virtual DOM
This is said to be one of the most important reasons why React app performances is very good
You know that Document Object Model or DOM is the tree representation of the HTML page
Cons of real DOM
Updating DOM is a slow and expensive process
You have to traverse DOM to find a node and update it
Updating in DOM is inefficient
Finding what needs to be updated is hard
Updating DOM has cascading effects - things need to be recalculated
Enter -> Virtual DOM
Virtual DOM is just a JavaScript object that represents the DOM nodes
Updating JavaScript object is efficient and fast
Virtual DOM is the blueprint of the DOM - the actual building
React listens to the changes via observables to find out which components changed and need to
be updated
Diffing
33 / 66
NgNinja Academy
34 / 66
NgNinja Academy
35 / 66
NgNinja Academy
36 / 66
NgNinja Academy
function MyComponent() {
function MyComponent() {
return (
<div>
<button onClick={() => setLanguage("javascript")}>
I love JS
</button>
<p>I love {language}</p>
</div>
);
}
37 / 66
NgNinja Academy
38 / 66
NgNinja Academy
39 / 66
NgNinja Academy
Web Development
40 / 66
NgNinja Academy
Once you feel you have come to a point where you need to save changes such that a history is
maintained for it - that's where you start the commit process
In the commit process you stage all your changes
git add command for staging changes
Then you write a nice commit message that will describe your changes to the code
ex: Added new TODO component
Then you commit your changes to your "local repository"
git commit command for committing your changes
At this point git history is generated for your commited changed. But the changes are still on your
local system
Then you push your changes to the remote repository
git push command for pushing the changes
git merge is used if you are already on the branch and just want to merge remote changes with
your local changes
NOTE: The exact syntax for these commands will be explained in the following sections
Install Git
You will first have to install Git to be able to use it
You can follow the steps from their official docs - https://git-scm.com/
Once you install verify it by running this command
git
42 / 66
NgNinja Academy
cd Ngninja
43 / 66
NgNinja Academy
44 / 66
NgNinja Academy
Method 1
David Walsh's method
It is easy to implement and itʼs effective
Images are loaded after the HTML content
However, you donʼt get the saving on bandwidth that comes with preventing unnecessary image
data from being loaded when visitors donʼt view the entire page content
All images are loaded by the browser, whether users have scrolled them into view or not
What happens if JS is disabled?
Users may not see any image at all
Add <noscript> tag with src property
<noscript>
<img
src="myImage.jpg"
alt="My Image"
width="300px" />
</noscript>
Example
Here we are selecting all the images with img[data-src] selector
Once the HTML is loaded we just replace the data-src attribute with src attribute which will
render the image
[].forEach.call(document.querySelectorAll('img[data-src]'), function(img)
{
img.setAttribute('src', img.getAttribute('data-src'));
img.onload = function() {
img.removeAttribute('data-src');
};
});
46 / 66
NgNinja Academy
47 / 66
NgNinja Academy
feJhbGciOizzUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQm9iYnkgVGFibGVzIiwiaWF0I
joxNTE2MjM5MDIyLCJpc0FkbWluIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJ1c2Vyc01ha2UiOnR
ydWUsInVzZXJzQmFuIjp0cnVlLCJ1c2Vyc0RlbGV0ZSI6ZdEsc2V9fQ.HFRcI4qU2lyvDnXhO-
cSTkhvhrTCyXv6f6wXSJKGblk
Header
48 / 66
NgNinja Academy
// Header example
{
"alg": "HS256",
"typ": "JWT"
}
Payload
This is the most important part from the app's perspective
Payload contains the claims
User sends this payload to the server
Server then decodes the payload to check for example whether the user can delete a
resource
// Payload example
{
"name": "Ninja",
"iat": 123422221, // timestamp the JWT was issued
"isAdmin": true,
"permissions": {
"canViewOrders": true,
"canDeleteOrders": false
}
}
Signature
This pertains to the security
Basically, it's a hashed value of your header, payload, and SECRET
The secret that only server and trusted entities know
Server used this signature to validate the JWT sent by the user
It looks gibberish
49 / 66
NgNinja Academy
Advantages of JWT
It is compact so transmission is fast
JSON is less verbose
It is self contained
The payload contains all the required information about user
So no need to query server more than once
It is very secure
It can use the shared SECRET as well as pub/private key pair
Strength of the security is strongly linked to your secret key
It is easy to implement
Developers use Auth0 to manage majority if the JWT stack
Disadvantages of JWT
Logouts, deleting users, invalidating token is not easy in JWT
50 / 66
NgNinja Academy
51 / 66
NgNinja Academy
52 / 66
NgNinja Academy
53 / 66
NgNinja Academy
54 / 66
NgNinja Academy
Week 1
HTML
Anatomy of HTML
history, what can HTML do
What is DOM
What are DOM nodes
Tags - HTML, Body, Head, Nav
Tags - Div, P, Section, Span
CSS
What is Box-model
Selectors - Id, Class, Element-tag
Properties - Color, Background-color, Border
Properties - Margin, Padding
Chrome Developer Tools
Elements tab
select DOM element through dev-tool
Inspect an element
Inspect styles of the element
Change styles through the inspect tool
Delete classes from the element
Add new class to the element through the dev tool
Exercise
. Create HTML page
. Add Navigation bar with items - Home, About, Contact Us
. Set background color of navigation bar to Blue
. Add a text paragraph inside Div element below navigation bar
55 / 66
NgNinja Academy
56 / 66
NgNinja Academy
Week 6
HTML
Radio buttons
Checkboxes
When to use Radio buttons vs When to use Checkboxes
CSS
Introduction - What and Why Flexbox
Flexbox properties
flex-direction
flex-wrap
just-content
align-items
JavaScript
Builtin Functions
Custom Functions and Methods
Write a custom function
Call that custom function from another function
Scoping in JavaScript
Function scope vs Block scope
What is hoisting
Git
Install Git locally
Use git -v command on command line
Merging remote branch X into your local branch Y
Deleting a local branch
Exercise
. Dynamically update the image source
. Create a dropdown list using flexbox and list elements
. Write a function to alert "Hello World" message
. Write a function that takes in a string and a number as arguments and console logs its value
. Find length of this string - "I love JavaScript"
. Change the case to all-capital for this string - "I love JavaScript"
57 / 66
NgNinja Academy
58 / 66
NgNinja Academy
59 / 66
NgNinja Academy
60 / 66
NgNinja Academy
Week 1
HTML
Refresh the tags
HTML, Body, Head, Nav
Div, P, Section, Span
Anchor, Images
,
Lists Tables
What is DOM
How does DOM work
What is DOM hierarchy
CSS
Box-model
Selectors - Id, Class, Element-tag
Properties - Color, Background-color, Border
Properties - Margin, Padding
Decoupling CSS and HTML
What and Why CDN
JavaScript
Primitive data types - Numbers, Boolean, String, etc
Complex data types - Array, Objects
Variables - var, let, const
What is the difference between them
When to use which type of declaration
Exercises
Write a function that takes in an array of mixed data types and returns the array in reverse
order
Implement a clickable image that will take you to your favorite website
61 / 66
NgNinja Academy
62 / 66
NgNinja Academy
Week 6
JavaScript
Immutable Data Structures
JavaScript as Functional Programming language
Currying
ReactJS
Installing ReactJS
What is JSX
What is a React component
Why do you need components
Creating your first React App
using create-react-app utility
Git
Cherry-picking a particular commit
Rebasing your branch with the other branch
Creating pull requests
Squashing multiple commits into one single commit
Chrome Developer Tools
Networks tab - All, XHR, JS
Networks tab - Inspect XHR query request
Networks tab - Inspect XHR query response
Exercises
Create a react application
Create a React component that displays details of a TODO task
task name, description, due dates, priority
Demonstrate cherry-picking a particular commit
Commit changes to branch X
Goto branch Y and cherry pick the last commit from branch X and push it to the
branch Y
63 / 66
NgNinja Academy
64 / 66
NgNinja Academy
65 / 66
NgNinja Academy
. Image carousal / slider beginner - Show left/right buttons to change the images - Have a timer to
change the images after X seconds - Pull images from a folder or from CDN - Build it using simple
HTML, CSS, JavaScript (without library/framework)
. Quote of the day beginner - Single page application - Show quote for the day - Change quote
every single day - Pull quote from a JSON file - Twist: or you can show a different quote every time
user refreshes the page - Twist 2: You can build similar app for Joke of the day
66 / 66