Ora

How do I start a react app with yarn?

Published in React Application Setup 5 mins read

To start a React app with Yarn, you'll typically use create-react-app, a popular toolchain that sets up a modern React development environment with no configuration.

Prerequisites

Before you begin, ensure you have the following installed on your system:

  • Node.js: create-react-app requires Node.js (version 14.0.0 or later). Node.js comes with npm (Node Package Manager), which you can use to install Yarn.
  • Yarn: A fast, reliable, and secure dependency management tool.

You can verify your Node.js and Yarn installations by running:

node -v
yarn -v

Step-by-Step Guide to Creating Your First React App with Yarn

Follow these steps to quickly get your React application up and running:

1. Install Yarn (If You Haven't Already)

If Yarn isn't installed globally, you can install it using npm:

npm install -g yarn

For alternative installation methods (e.g., Homebrew for macOS), refer to the official Yarn documentation.

2. Create Your React Project

Use the yarn create command, which is a convenient way to create new projects from templates. For React, you'll use create-react-app:

yarn create react-app my-react-app
  • Replace my-react-app with your desired project name. This command will create a new directory with that name and set up all the necessary files and dependencies.
  • If you want to use TypeScript from the start, you can specify a template:
    yarn create react-react-app my-typescript-app --template typescript

This process might take a few minutes as it downloads and installs all initial dependencies.

3. Navigate into Your Project Directory

Once the project creation is complete, change your current directory to your new project:

cd my-react-app

4. Start the Development Server

Inside your project directory, run the start script:

yarn start

This command will:

  • Start a local development server (usually on http://localhost:3000).
  • Open your default web browser to display your new React application.
  • Provide hot-reloading, meaning any changes you save in your code will automatically update in the browser without needing a manual refresh.

Congratulations! You've successfully started a React app with Yarn.

Essential Yarn Commands for React Development

Here's a quick reference for common Yarn commands you'll use in your React project:

Command Description
yarn start Starts the development server. Your app will open in your browser, and changes will hot-reload. Use this for local development.
yarn build Creates a production-ready build of your app in the build folder. This optimized, static bundle is ready for deployment.
yarn test Runs the test runner in interactive watch mode.
yarn eject Removes the single build dependency from your project. This allows you to customize configuration files and build scripts, but it's a one-way operation that cannot be undone. Generally, avoid unless absolutely necessary.
yarn add [package-name] Adds a new package (dependency) to your project. E.g., yarn add axios to install the Axios HTTP client.
yarn remove [package-name] Removes a package from your project's dependencies.

Understanding the React App Build and Deployment Process

When you run yarn build, create-react-app compiles your source code into highly optimized static files (HTML, CSS, JavaScript, and assets) that can be served by any static web server. This build process is crucial for deploying your application to production environments.

The Multi-Stage Build Strategy for Production

For advanced deployments, especially within containerized environments like Docker, a multi-stage build strategy is often employed. This approach efficiently packages your application while keeping the final deployment image lightweight:

  1. Initial Build Environment: The process begins by spinning up a robust container environment, often based on a Node.js/Yarn image. This initial stage provides all the necessary tools and libraries to build your React application.
  2. Dependency Installation: Within this build container, your project's package.json and yarn.lock files are copied, and all required development and production dependencies are installed (yarn install).
  3. Source Code and Static Files Generation: Your entire application source code is then copied into the container. The yarn build command is executed, which compiles your React components, optimizes assets, and generates the final, minimized static files in a dedicated build directory.
  4. Discarding the Build Environment: Once the static files are generated, the large Node.js image with all its development dependencies is discarded. This is a critical step for efficiency, as the development tools are no longer needed to serve the application.
  5. Serving the Static Files: A new, much smaller and more secure container image (e.g., an Nginx image) is then initiated. The static build results created in the previous stage are copied into this new, minimal container. This final, lightweight image contains only what's necessary to serve your compiled React application effectively.

This multi-stage process ensures that your deployed application is lean, secure, and performant, avoiding unnecessary overhead from development tools.

Tips for a Smooth React Development Workflow

  • IDE Choice: Use a powerful Integrated Development Environment like Visual Studio Code for excellent React support, extensions, and debugging tools.
  • Explore Project Structure: Familiarize yourself with the generated project structure, especially the src folder where your main application code resides.
  • Learn React Fundamentals: Focus on understanding React's core concepts: components, JSX, props, state, and hooks.
  • Version Control: Integrate your project with Git from the beginning to manage changes and collaborate effectively.

Next Steps After Starting Your App

Once your app is running, you can start modifying the src/App.js file, which is the main component of your application. Experiment with:

  • Creating new components.
  • Adding styling (CSS modules, styled-components).
  • Implementing routing with React Router.
  • Managing application state with hooks like useState and useReducer, or external libraries like Redux or Zustand.