📖 Deeper dive reading: MDN Introduction to client-side frameworks
Web frameworks seek to make the job of writing web applications easier by providing tools for completing common application tasks. This includes things like modularizing code, creating single page applications, simplifying reactivity, and supporting diverse hardware devices.
Some frameworks take things beyond the standard web technologies (HTML, CSS, JavaScript) and create new hybrid file formats that combine things like HTML and JavaScript into a single file. Examples of this include React JSX, Vue SFC, and Svelte files. Abstracting away the core web file formats puts the focus on functional components rather than files.
There are lots of web frameworks to choose from and they evolve all the time. You can view the latest popularity poll at StateOfJS.
- Source: StateOfJS web framework poll
Each framework has advantages and disadvantages. Some are very prescriptive (opinionated) about how to do things, some have major institutional backing, and others have a strong open source community. Other factors you want to consider include how easy it is to learn, how it impacts productivity, how performant it is, how long it takes to build, and how actively the framework is evolving.
For our classwork we will use the web framework React. However, before we dig into React let's look at how the major frameworks would render a simple hello world application.
Vue combines HTML, CSS, and JavaScript into a single file. HTML is represented by a template element that can be aggregated into other templates.
SFC
<script>
export default {
data() {
return {
name: 'world',
};
},
};
</script>
<style>
p {
color: green;
}
</style>
<template>
<p>Hello {{ name }}!</p>
</template>Like Vue, Svelte combines HTML, CSS, and JavaScript into a single file. The difference here is that Svelte requires a transpiler to generate browser-ready code, instead of a runtime virtual DOM.
Svelte file
<script>
let name = 'world';
</script>
<style>
p {
color: green;
}
</style>
<p>Hello {name}!</p>React combines JavaScript and HTML into its component format. CSS must be declared outside of the JSX file. The component itself leverages the functionality of JavaScript and can be represented as a function or class.
JSX
import 'hello.css';
const Hello = () => {
let name = 'world';
return <p>Hello {name}</p>;
};CSS
p {
color: green;
}An Angular component defines what JavaScript, HTML, and CSS are combined together. This keeps a fairly strong separation of files that are usually grouped together in a directory rather than using the single file representation.
JS
@Component({
selector: 'app-hello-world',
templateUrl: './hello-world.component.html',
styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent {
name: string;
constructor() {
this.name = 'world';
}
}HTML
<p>hello {{name}}</p>CSS
p {
color: green;
}