Advanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts
By Mehul Mohan
()
About this ebook
Related to Advanced Web Development with React
Related ebooks
Getting Started with React Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratingsObject Oriented Programming with Angular: Build and Deploy Your Web Application Using Angular with Ease ( English Edition) Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsJavaScript for Modern Web Development: Building a Web Application Using HTML, CSS, and JavaScript Rating: 0 out of 5 stars0 ratingsReact.js Design Patterns: Learn how to build scalable React apps with ease (English Edition) Rating: 0 out of 5 stars0 ratingsExploring Web Components: Build Reusable UI Web Components with Standard Technologies (English Edition) Rating: 0 out of 5 stars0 ratingsVue.js for Jobseekers: A complete guide to learning Vue.js, building projects, and getting hired (English Edition) Rating: 0 out of 5 stars0 ratingsNext.js Cookbook: Learn how to build scalable and high-performance apps from scratch (English Edition) Rating: 0 out of 5 stars0 ratingsBuilding a RESTful Web Service with Spring Rating: 5 out of 5 stars5/5Decoding JavaScript: A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition) Rating: 0 out of 5 stars0 ratingsWeb Development with MongoDB and NodeJS - Second Edition Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsEnterprise Applications with C# and .NET: Develop robust, secure, and scalable applications using .NET and C# (English Edition) Rating: 0 out of 5 stars0 ratingsASP.NET Core for Jobseekers: Build Career in Designing Cross-Platform Web Applications Using Razor and Entity Framework Core Rating: 0 out of 5 stars0 ratingsDesigning Microservices using Django: Structuring, Deploying and Managing the Microservices Architecture with Django Rating: 0 out of 5 stars0 ratingsReactive State for Angular with NgRx: Learn to build Reactive Angular Applications using NgRx (English Edition) Rating: 0 out of 5 stars0 ratingsReal-World Next.js: Build scalable, high-performance, and modern web applications using Next.js, the React framework for production Rating: 0 out of 5 stars0 ratings100+ Solutions in Java: A Hands-On Introduction to Programming in Java (English Edition) Rating: 0 out of 5 stars0 ratingsCode with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsJasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratings
Internet & Web For You
How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsSurveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsNo Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsWireless Hacking 101 Rating: 5 out of 5 stars5/5Cybersecurity All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsThe Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5How To Make Money Blogging: How I Replaced My Day-Job With My Blog and How You Can Start A Blog Today Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Ultimate guide for being anonymous: Avoiding prison time for fun and profit Rating: 5 out of 5 stars5/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Podcasting For Dummies Rating: 4 out of 5 stars4/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratings
Reviews for Advanced Web Development with React
0 ratings0 reviews
Book preview
Advanced Web Development with React - Mehul Mohan
CHAPTER 1
React 101
JavaScript: It is the programming language of the web. Since the dawn of the web, developers are eager to provide increasingly better experiences for the end-user in terms of interactivity, animations, effects, and stability. JavaScript made it all possible. As it progressed, there were needs for some obvious reasons to introduce frameworks and libraries, for faster development and better stability.
React is one such part of the ecosystem. React sells itself as A JavaScript library for building user interfaces
. But honestly, while being true to the statement, we think React goes much beyond that. It could be made into a complete ecosystem, powering some of the most complex web application systems, for example, Facebook.
We’ll be quickly going through the React basics and then taking up intermediate to advanced topics, which would really give you the essence of React and eventually help you to start writing your production-ready applications with React.
Very simply speaking, React is a User Interface (UI) library. Let’s break that down:
React, on its own says that it’s a library, which means that it does not try to ship everything with it. Instead, you have the option to cherry-pick the things you want in your project and can implement them.
React says that it is a UI library, that is used for building interfaces visible to the user. What does that mean? In simple terms, it means creating and maintaining what the user sees on the screen.
Structure
Introduction
Understanding React
Basic understanding of how React works as a UI library
Declarative versus imperative programming
Cheat-sheet for further React topics
Objectives
In this introductory chapter, we’ll be understanding what exactly is React doing and why should one care. We’ll be seeing on the surface, how it works as a UI library and how the declarative approach of React is so powerful in creating better UI systems. Finally, we’ll want to get started with React programming, but would end the chapter with some notes that might be handy to see as a JavaScript refresher.
React is component-based
React makes use of components. The ideology is simple - React says that instead of coding everything at the same place (like we usually do while coding simple HTML pages), break different (and usually independent) parts of the page into different components. This increases the code maintainability by factors and allows developers to have faster and clean codebases. We’ll have dedicated chapters on working with components, as it is the crux of React.
React is declarative
We’ll focus mainly on two programming paradigms here -declarative programming and imperative programming. Let’s start with a simple example.
When you write a code, you can specify:
What do you want to do?
How do you want to do?
If you specify both, you’re making use of the imperative programming practice. If you’re just specifying what you want to do, then you’re using the declarative way of programming. Here’s a simple example.
Consider that you want to draw a border around a box. In CSS, you’d simply say border: 2px solid black. This is declarative. You just said what you want to do, not how you want to do. On the other hand, if you try to draw the border in vanilla JavaScript, you have to specify four coordinates, and then draw the line manually using code. This would be an imperative way.
Declarative programming has its own pros and cons, but the pros almost always outweigh the cons:
Declarative code can make use of highly optimized algorithms. Since the how
is abstracted from the end-user, the imperative model under declarative code can make use of seriously complex code to provide major performance boosts, while exposing an easy declarative way for users to use the same functionality at the same time.
Declarative programming brings other benefits like code scalability, reduced bugs, and more understandable code.
Coming back to React, it is declarative. This means that you just tell React what
you want to do, and not how
you want to do it. React, under the hood, manages everything for you in a very optimized way.
We will discuss on how the React virtual DOM, reconciliation and other optional advanced stuff works under the hood, in detail, in the last chapter.
Quick JS revision
Before we dive into the React ecosystem, we’d like to put some quick JS things that you should definitely be aware of, while working with React. Let’s take a quick look at this section for now and come back later as you need.
this
this is surprisingly complicated for a lot of people in JavaScript. Keep in mind that in a function, this always refers to how that function is called, except for arrow functions, where this is lexically scoped:
function myName() {
return this.myname
}
const myNameButArrow = () =>console.log(this.myname)
const person1 = { myname: Mehul
, myName }
const person2 = { myname: James
, myName }
const person3 = { myname: Enzo
, myName: myNameButArrow }
console.log(person1.myName())
console.log(person2.myName())
console.log(person3.myName())
The output is showcased in the following screenshot:
Figure 1.1: Output
Arrow functions
Arrow functions allow you to write smaller functions, lexical scoping to this, and cannot be instantiated with a new keyword:
const obj = {
myFunction: function() {
return this
},
coolFunction: () => {
return this
}
}
console.log(obj.myFunction()) // logs the original obj
console.log(obj.coolFunction()) // logs window (global object)
Here we see the same preceding code with its output as showcased below. As stated earlier, the first statement logs the original object, and the second statement logs the whole window object:
Figure 1.2: Output
.map
.map allows you to manipulate an array without mutating the original array -that is, .map works on every element in an array and returns to you a new array, consisting of the elements you return from within the function passed in .map.
Here’s a quick example of how maps would work in this case:
const arr = [{
name: Mehul Mohan
,
country: India
}, {
name: James Paul
,
country: USA
}]
const component = arr.map(name =>arr.name)
console.log(component) // logs [Mehul Mohan
, James Paul
]
console.log(arr) // logs original object
The preceding code logs the changed array, as you would expect the map function to do. This, however, does not modify the original array.
.reduce
.reduce allows you to reduce the array to a single value. This is achieved with an accumulator, and the current value it is iterating over. Let’s look at an example to understand:
x = [1,2,3,4,5]
x.reduce((acc, val) => acc + val, 0)
In the preceding example, reduce accepts a function thatis called every time with two values: the accumulator, and the current value. Whatever you return from the function, becomes the accumulator value for the next function call. Optionally, you can pass the initial accumulator value as the second argument to the .reduce function. This, if not present, would initialize the accumulator with the first element in the array, and would start reducing the array from the second value.
Classes
Classes in ES6 is merely syntactic sugar for functions. JavaScript is not OOP. It follows the prototypal inheritance model, and that’s what classes do as well. Although, the proposed syntax makes it look very similar to OOP.
Here’s an example of ES6 class:
class ILoveNumbers {
constructor(num) {
this.anothernum = num
}
getNum() {
console.log(this.anothernum)
}
};
class IHateNumbers extends ILoveNumbers {
constructor(num) {
super(num**num)
this.num = num
}
getNum() {
console.log(this.num)
}
getNumFromParent() {
console.log(super.getNum())
}
};
let obj = new IHateNumbers(5)
obj.getNum() // 5
obj.getNumFromParent() // 3125
Some important points to note aboutthe preceding code:
Here we created a simple class called ILoveNumbers, which just stores a number.
We created another class,IHateNumbers, which extends from our previous class. Extending
in class-based syntax merely means that you’re adding the parent class (function) to the prototypal chain of the current class (function).
We make use of a special keyword, called super. Using super, we can access the parent class. Remember, when using super in a method, super refers to the prototype chain of the object it was defined with. Thismeans, if I change the prototype chain of IHateNumbers to something else, the super.getFav() should not work as expected. Consider the following code:
class C1{ method() { return 1 } }
class C2 extends C1{ method() { return super.method() } }
class C3{ method() { return 3 } }
class C4 extends C3{ method() { return 4 } }
const obj = new C4
console.log(obj.method()) // outputs 4
C4.prototype.method = C2.prototype.method
console.log(obj.method()) // outputs 1
Here you can clearly see that super.method() does not call the method of C3., Instead, it calls the method of C1. However, if you would have defined the method of C4 to return super.method(), it would call the method of C3.
Closures
Closure is a fundamental concept implemented in JavaScript that is extremely powerful and beautiful at the same time. To define closure in one line,:the function remembers. We have a common understanding as a Computer Scientist that when functions return, they remove all the local variable information associated with them. This is changed when you use a closure. For example, consider the following piece of code:
function a() {
let value = 1
return () => value++
}
const func = a()
func() // 1
func() // 2
func() // 3
In the preceding function, we can see that although value is a local variable, it is not destroyed when the execution of function a is completed. Instead, the function