The developer experience for Next.js vs. React is similar in some ways and drastically different in others.
When choosing any software library or framework, one normally considers the developer experience. When I say “developer experience” here, I mean to say what it’s like for developers to actually do the task. Developers generally favor libraries or frameworks that are fun and easy to use. This is a major reason why we have the leading libraries and frameworks today.
In the world of React, Next.js has become a popular framework for “hitting the ground running.” As a React fan myself, I picked up Next.js a few months ago and have really enjoyed working with it on my projects. I liked it so much, that I even rewrote my personal blog using Next.js (check out my post on it).
Next.js builds on top of React to provide a streamlined development experience. There is a small learning curve with Next.js, but even developers new to the world of frontend can get up and running relatively quickly. That being said, there is definitely a different experience when building a project with Next.js vs. React.
This post compares the developer experience of Next.js vs. React. I’ll walk through some background first and then dive into more specifics, discussing what it’s like to initially start a project, build pages, retrieve data, use the documentation, and perform advanced actions with both Next.js and React.
I’ll be referring to a sample project that you can find in my GitHub repo here. This project shows two different implementations of a fan site about the hit show The Mandalorian. The react
folder in the project is the React version, of course, and the next.js
folder contains the Next.js version. Both projects work and should only need the standard npm install
to get up and going.
Before we dive into the actual developer experience, it helps to have some background.
React was originally created by Facebook and has become one of the most popular libraries in the frontend world today. React is easily extendable and can include features like routing as well as state management patterns with libraries like Redux. React is minimal in its footprint but can be customized for almost any project. For more about React on a high level, check out the official React documentation.
Next.js was created on top of React in an effort to build an easy-to-use development framework. It was developed by Vercel (formerly Zeit) and makes use of many of the popular features of React. Right out of the box, Next.js provides things like pre-rendering, routing, code splitting, and webpack support. For more on Next.js, check out the official Next.js documentation.
With React, you can get up and running by installing Node.js on your machine and running npx create-react-app my-app
. This will create a basic project structure with src/App.js
file as the entry point for the application.
You’ll also have a public
folder where you can store assets, and the initial scaffold includes a service worker and a method to pull in Jest for testing. The initial scaffold looks like this:
.
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ ├── serviceWorker.js
│ └── setupTests.js
└── yarn.lock
With Next.js, you can get started by running npx create-next-app
. This will scaffold out a project that already has a pages
folder for the pages or routes and a public
directory that hosts your assets. The initial scaffold looks like this:
.
├── README.md
├── package.json
├── node_modules
├── pages
│ ├── _app.js
│ ├── api
│ └── index.js
├── public
│ ├── favicon.ico
│ └── vercel.svg
├── styles
│ ├── Home.module.css
│ └── globals.css
└── yarn.lock
The files in the pages
directory correlate to the routes in your application. The public
directory holds your static files or images you want to serve, and it can be directly accessed — no need to use require
or other traditional React methods to import pictures into components.
Within the pages
directory, you’ll see an index.js
file, which is the entry point of your application. If you want to navigate to other pages, you can use the router with Link
, as you see here:
<div className="header__links">
<Link href="/">
<a className="header__anchor">Home</a>
</Link>
<Link href="/about">
<a className="header__anchor">About</a>
</Link>
</div>
With regards to the developer experience, the initial scaffolding process is pretty straightforward for both Next.js and React. React, however, does require you to add libraries like React Router for routing, whereas Next.js offers that functionality out of the box with the Link
component.
Additionally, the overall structure of your application is already guided by Next.js by having the pages
directory to hold your containers etc.
Now we can begin to discuss real examples of React vs. Next.js with the sample application I mentioned at the beginning. Again, you can find it in the repo.
Building pages with React requires you to create a component and then pull in React Router to orchestrate transitions in your site. If you look in the react
folder in the sample application, you’ll see what you would likely expect from a traditional React application:
export default function App() {
return (
<Router>
<section>
<Header />
<Switch>
<Route path="/episodes">
<EpisodesPage />
</Route>
<Route path="/season2">
<Season2Page />
</Route>
<Route path="/quotes">
<QuotesPage />
</Route>
<Route path="/">
<HomePage />
</Route>
</Switch>
</section>
</Router>
);
}
Here, the Header
, EpisodesPage
, Season2Page2
, QuotesPage
, and HomePage
are all components that React Router is routing the URL path to render.
If you look at the Next.js
folder of the project, you’ll notice that the project is much leaner because the routes are all built into the pages
folder. The Header
component uses Link
to route to the different pages, as you see here:
import Link from "next/link";
const Header = () => {
return (
<nav className="header">
<span>
<Link href="/">Home</Link>
</span>
<span>
<Link href="/episodes">Episodes</Link>
</span>
<span>
<Link href="/season2">Season 2</Link>
</span>
<span>
<Link href="/quotes">Quotes</Link>
</span>
</nav>
);
};
export default Header;
A high-level view of the Next.js project shows how easy it is to follow as well:
.
├── README.md
├── package-lock.json
├── package.json
├── pages
│ ├── _app.js
│ ├── _document.js
│ ├── components
│ │ └── Header.js
│ ├── episodes.js
│ ├── index.js
│ ├── quotes.js
│ ├── season2.js
│ └── styles
│ ├── _contact.scss
│ ├── _episodes.scss
│ ├── _header.scss
│ ├── _home.scss
│ ├── _quotes.scss
│ ├── _season2.scss
│ └── styles.scss
├── public
│ ├── HomePage.jpg
│ └── favicon.ico
└── yarn.lock
When you want to build out pages for the React project, you must build the component and then add it to the router. When you want to build pages for the Next.js project, you just add the page to the pages
folder and the necessary Link
to the Header
component. This makes your life easier because you’re writing less code, and the project is easy to follow.
#next #react #javascript #developer #web-development