Introduction
A page is displayed using HTML. On top of that, CSS defines the styling of the HTML tags—in other words, how they look—and JavaScript controls how the HTML and CSS behave dynamically.
When HTML tags are rendered on the web, they are all rendered into a DOM structure. The nesting relationships between HTML tags also become concrete parent-child relationships between DOM elements.
In traditional, everyday web pages, we generally use imperative programming to define how HTML elements change—what a component should look like in a given situation, what content should be modified, and so on.
Problems with Traditional Component Development (Imperative Programming)
React, by contrast, introduced a declarative programming approach to replace traditional imperative programming. What are the benefits?
Let's look first at the drawbacks of traditional programming.
Traditional imperative programming has the following drawbacks:
- The code is verbose and prone to edge cases. Do all the branches cover every possible user action?
- It's hard to follow: you have to work through all the code step by step to understand what a component change means.
- Code with excessive coupling between components is hard to reuse.
Of course, there are advantages—namely speed. If your skill level is high enough, you'll naturally get that benefit. But for ordinary developers like us, these drawbacks far outweigh the advantages.
What Is Declarative Programming?
When we write frontend components, we no longer focus on the specific process. Instead, we focus on what kind of component we want. For example, imperative programming is like telling a chef how to make a cheese pizza step by step: first, mix the flour; second, ...
Declarative programming, by contrast, is simply saying, “I want a cheese pizza.”
Of course, these benefits don't come all at once. First, you'll inevitably run into concepts you've never heard of: component, props, state, and JSX. All of these are there to help us better declare what we want. Don't worry too much—take a look at the React concepts tutorial from Next.js. I'm sure you'll get the hang of it quickly.
So What Is Next.js?
Put simply, our frontend pages aren't made up only of components. They also need features like navigating between pages, how page data changes, how rendering works, how pages are routed, how data is fetched, and so on.
The Next.js framework helps us implement these features better and makes it easier to write application code.