A front-end timeline
Some noteworthy front end libraries that have been used in the past few years:
React manages the creation and updating of DOM nodes in your Web page.
React is unopinionated
JSX : Javascript Extension is a language invented to help write React Applications (looks like a mixture of JS and HTML)
Here is an overview of the difference between rendering out vanilla JS to create elements, and JSX:
fetch("https://example.com/api/people")
.then((response) => response.json())
.then((people) => {
const html = "<ul>";
for (let person of data.people) {
html += `<li>${person.lastName}, ${person.firstName}</li>`;
}
html += "</ul>";
document.querySelector("#people-list").innerHTML = html;
});
function PeopleList(props) {
return (
<ul>
$
{props.people.map((person) => (
<li>
{person.lastName}, {person.firstName}
</li>
))}
</ul>
);
}
const peopleListElement = document.querySelector("#people-list");
fetch("https://example.com/api/people")
.then((response) => response.json())
.then((people) => {
const props = { people };
ReactDOM.render(<PeopleList props={props} />, peopleListElement);
});
Using tools with React
React DevTools
: New tool in your browser to see ow React is working in the browsercreate-react-app
: Extensible command-line tool to help generate standard React applications.Webpack
: In between tool for dealing with the extra build step involved.HMR : (Hot Module Replacement) When you make changes to your source code the changes are delivered in real-time.
React Developers created something called Flux Architecture
to moderate how their web page consumes and modifies data received from back-end API’s.
Choosing React
There are many benefits to using React over just Vanilla JS.
Modularity
Easy to start
createElement
method in React.Declarative Programming
Reusability
One-flow of data
React apps are built as a combination of parent and child components.
Virtual DOM
: React provides a Virtual DOM that acts as an agent between the real DOM and the developer to help debug, maintain, and provide general use.
Exporting one item per file
export default
statement in ES6 to export an item.ES6
export default class Wallet {
// ...
}
// sayHello will not be exported
function sayHello() {
console.log("Hello!");
}
CommonJS (Equivalent)
class Wallet {
// ...
}
// sayHello will not be exported
function sayHello() {
console.log("Hello!");
}
module.exports = Wallet;
Exporting multiple items per file
export
keyword (without default) to export multiple items per file.ES6 (Better to export them individually like this, rather than bunching them all into an object)
export class Wallet {
// ...
}
export function sayHello() {
console.log("Hello!");
}
export const sayHi = () => {
console.log("Hi!");
};
class Wallet {
// ...
}
function sayHello() {
console.log("Hello!");
}
const sayHi = () => {
console.log("Hi!");
};
export { Wallet, sayHello, sayHi };
CommonJS (Equivalent)
class Wallet {
// ...
}
function sayHello() {
console.log("Hello!");
}
const sayHi = () => {
console.log("Hi!");
};
module.exports = {
Wallet,
sayHello,
sayHi,
};
Importing with ES6 vs CommonJS
ES6
CommonJS
Unnamed default imports
// exporting
export default class Wallet {
// ...
}
// importing
import Money from "wallet.js";
const wallet = new Money();
export
instead of export default
then your import is already named and cannot be renamed.// exporting
export class Wallet {
// ...
}
// importing
import { Wallet } from "wallet.js";
const wallet = new Wallet();
Aliasing imports
as
keyword to refer to it later.// export
export function sayHello() {
console.log("Hello!");
}
export const sayHi = () => {
console.log("Hi!");
};
//import
import * as Greetings from "greetings.js";
Greetings.sayHello(); // Hello!
Greetings.sayHi(); // Hi!
import { Wallet as W1 } from "./wallet1";
import { Wallet as W2 } from "./wallet2";
const w1 = new W1();
const w2 = new W2();
Browser support for ES6 Modules
<script type="module" src="./wallet.js"></script>