Learn Remix Β· 1/13

Welcome to Remix: The Web, Reimagined

8 min read

Welcome to Remix: The Web, Reimagined πŸ’Ώ

Hey there, future Remix developer! πŸ‘‹ Welcome to this guide where we'll explore the fundamentals of Remix. If you've ever felt that building modern web apps can sometimes feel like you're trying to assemble a spaceship with a million tiny, confusing parts, you're in the right place. Remix is here to make web development feel fun and productive again.

So, grab your favorite beverage, get comfy, and let's dive into what makes Remix so special.


The "Why": React, Server-Side Rendering, and the Remix Philosophy πŸ€”

The Problem:

In the world of React, a common approach to building applications is using what's called a Single Page Application (SPA). With a SPA, your browser downloads a big bundle of JavaScript, and then React takes over to render the user interface. This is great for creating interactive, app-like experiences.

However, this approach has a couple of not-so-great side effects:

  • Slow Initial Load: The user has to wait for all that JavaScript to download and run before they see anything meaningful. It's like ordering a book online and having to wait for the entire library to be delivered before you can read the first page.
  • SEO Challenges: Search engines have gotten better at reading JavaScript, but it can still be a bit of a gamble. Sometimes, they see a blank page before your content loads, which isn't great for ranking.

The Solution: Server-Side Rendering (SSR) to the Rescue!

To solve this, developers started using Server-Side Rendering. With SSR, when you visit a website, the server renders the initial HTML and sends it to your browser. This means the user sees content almost immediately. Then, the JavaScript loads in the background and "hydrates" the page, making it interactive.

So, Where Does Remix Fit In?

Remix is a full-stack web framework built on top of React that embraces the power of the web platform and server-side rendering from the ground up. It's not just about rendering on the server; it's about leveraging the client-server model to its full potential.

The Remix Philosophy πŸ“œ

The core philosophy of Remix can be boiled down to a few key ideas:

  • Embrace Web Standards: Remix is built on top of web standards like the Fetch API, Request, and Response objects. This means that if you know how to work with these, you're already on your way to knowing Remix.
  • Data Loading is a First-Class Citizen: In Remix, data loading is deeply integrated into its routing system. This eliminates the need for complex state management libraries for fetching data.
  • Progressive Enhancement: Remix applications are designed to work with and without JavaScript. This means your site is accessible to more users and is more resilient to network issues.
  • Focus on the User Experience: By leveraging the server, Remix aims to deliver faster page loads, smoother transitions, and a more robust user experience.

Imagine you're at a restaurant. In a typical SPA world, you'd get a menu (the initial JavaScript) and then have to go to the kitchen yourself to get each dish (data fetching). With Remix, the waiter (the server) takes your order (the route you visit) and brings you the fully prepared meal (the rendered page with data). It's a much more streamlined experience!


Setting up the Environment: Creating Your First Remix App πŸ’Ώ

Alright, enough talk, let's get our hands dirty and create our very first Remix application. Don't worry, the process is smoother than a fresh jar of peanut butter.

Prerequisites:

Before we begin, make sure you have the following installed on your machine:

  • Node.js (the latest LTS version is recommended)
  • npm or another package manager like yarn or pnpm

Let's Get Building!

The Remix team has made it incredibly easy to get started with a new project. Just open up your terminal and run the following command:

bash
npx create-remix@latest

This command will kick off an interactive setup process. It will ask you a few questions to configure your new project:

  1. Where would you like to create your app? You can enter a name for your new project directory here. Let's call it my-remix-app.
  2. What type of app do you want to create? You'll see a few options. For now, let's stick with the recommended "Just the basics" to keep things simple.
  3. Where do you want to deploy? Remix gives you a variety of deployment targets. Let's choose "Remix App Server" for local development.
  4. TypeScript or JavaScript? We'll go with JavaScript for this guide, but feel free to choose TypeScript if you're comfortable with it.
  5. Do you want me to run npm install? Go ahead and say yes to this.

And that's it! The command will work its magic and create a new Remix project for you.

Running Your App

Once the installation is complete, navigate into your new project directory:

bash
cd my-remix-app

And then start the development server:

bash
npm run dev

This will fire up a local development server, and you can now open your browser and navigate to http://localhost:3000. You should be greeted with the default Remix welcome page. πŸŽ‰


Project Anatomy: Understanding the Folder Structure πŸ“‚

Now that we have a Remix app up and running, let's take a peek under the hood and understand what all those generated files and folders are for.

A typical Remix project has the following structure:

stylus
my-remix-app/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── _index.tsx
β”‚   β”œβ”€β”€ root.tsx
β”‚   └── entry.client.tsx
β”‚   └── entry.server.tsx
β”œβ”€β”€ public/
β”‚   └── favicon.ico
β”œβ”€β”€ package.json
└── remix.config.js

Let's break down the most important parts:

  • app/: This is the heart of your Remix application. All of your application code, like routes and components, will live here.

    • routes/: This is where the magic of Remix's file-system-based routing happens. Each file in this directory corresponds to a route in your application. We'll dive deeper into this in the next section.
    • root.tsx: This file is the root of your entire application. It's where you define the overall HTML document structure (<html>, <body>, etc.). Think of it as the main template for your entire site.
    • entry.client.tsx: This is the first bit of your code that runs in the browser. It's responsible for "hydrating" the server-rendered HTML and making your application interactive. You'll rarely need to touch this file.
    • entry.server.tsx: This is the code that runs on your server to handle requests and render the initial HTML. You typically won't need to modify this either.
  • public/: This directory is for all your static assets, like images, fonts, and your favicon.ico. Anything in here will be publicly accessible.

  • package.json: This is your standard Node.js project file that manages your project's dependencies and scripts.

  • remix.config.js: This file contains the configuration for your Remix application. You can use it to customize things like the server port and other build settings.

An Interesting Fact: The idea of file-system-based routing isn't new to Remix. It was popularized by frameworks like Next.js and has become a standard in modern web development for its simplicity and intuitiveness.


Hello Remix: Writing Your First Route and Component πŸ‘‹

Now for the fun part! Let's create our very first custom route and React component. We're going to make a simple "About Us" page.

Step 1: Create a New Route

Inside the app/routes/ directory, create a new file named about.jsx. The name of this file is important because it will directly map to the URL. In this case, this file will be accessible at http://localhost:3000/about.

Step 2: Create a React Component

Now, open up your newly created app/routes/about.jsx file and add the following code:

jsx
export default function About() {
  return (
    <div>
      <h1>About Us</h1>
      <p>This is the about page for our awesome Remix application!</p>
    </div>
  );
}

Let's break this down:

  • export default function About() { ... }: This is a standard React functional component.
  • The JSX inside the return statement is what will be rendered to the screen when someone visits the /about route.

Step 3: See it in Action!

Save the file and head back to your browser. Now, navigate to http://localhost:3000/about. You should see your newly created "About Us" page!

How it Works Under the Hood:

When you navigate to /about, Remix's server looks inside the app/routes/ directory for a file that matches that route. It finds about.jsx, renders the About component to HTML, and sends that HTML back to your browser. It's that simple!

Let's Add a Link

It's not very user-friendly to have to type the URL manually. Let's add a link from our homepage to the about page.

Open up app/routes/_index.jsx (this is your homepage). You'll see some boilerplate code. Let's replace it with this:

jsx
import { Link } from "@remix-run/react";

export default function Index() {
  return (
    <div>
      <h1>Welcome to Remix!</h1>
      <Link to="/about">Go to the About Page</Link>
    </div>
  );
}

Here, we're importing the Link component from @remix-run/react. This is Remix's special component for creating links between routes. Using <Link> instead of a regular <a> tag allows Remix to handle the navigation on the client-side without a full page reload, making your application feel much faster.

Now, go back to your homepage at http://localhost:3000, and you should see a link to the "About Page". Click it, and you'll be instantly taken to your about page. Pretty cool, right?

And there you have it! You've successfully set up a Remix application, understood its structure, and created your first route and component. You're well on your way to building amazing things with Remix! πŸš€


Further Reading & References πŸ“š

Challenge

You are tasked with building the basic route structure for a new Remix application. In the editor below, you will find placeholders for two separate route files: app/routes/_index.jsx (the homepage) and app/routes/contact.jsx (the contact page).

Your goal is to write the full React component code for each of these files.

Requirements:

  1. Homepage (app/routes/_index.jsx):

    • It must display a level-one heading (<h1>) with the text "Welcome to the Main Page".
    • It must include a navigation link to the Contact page (/contact). Use the specific Remix component designed for client-side navigation.
  2. Contact Page (app/routes/contact.jsx):

    • It must display a level-one heading (<h1>) with the text "Contact Us".
    • It must include a navigation link back to the Homepage (/).

Expected result

The final code should define two complete React functional components, separated by the separator comment.

For the app/routes/_index.jsx section:

  • It must import Link from @remix-run/react.
  • It must define a function component (e.g., Index) and export it as the default.
  • The component must return JSX containing an <h1> with the text "Welcome to the Main Page" and a <Link> component with its to attribute set to "/contact".

For the app/routes/contact.jsx section:

  • It must import Link from @remix-run/react.
  • It must define a function component (e.g., Contact) and export it as the default.
  • The component must return JSX containing an <h1> with the text "Contact Us" and a <Link> component with its to attribute set to "/".
jsx

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.