Tooling: scaffolding your first project

Create React App (CRA) promises a working development environment with no configuration on your part. Under the hood it relies on two packages: webpack, a bundler that optimizes project assets such as JavaScript, CSS and images, and Babel, which lets you write newer JavaScript features even where browsers don't yet support them. Both are sound choices, but newer tools now do the job better — notably Vite and Speedy Web Compiler (SWC) — and they are faster and easier to configure. That configurability matters: adjusting a CRA setup is difficult without ejecting.

To start a project with Vite, you need Node version 12 or higher installed, then run:

npm create vite

After choosing a project name, select React from the framework list and then either Javascript + SWC or Typescript + SWC. Change into the project directory with cd and run:

npm i && npm run dev

A development server should then be available at localhost:5173.

Default values for props

Props are how data reaches a React component; they are attached like HTML attributes and read from the prop object passed as an argument.

// App.jsx
export default function App() {
  return <Card title="Hello" description="world" />
}

// Card.jsx
function Card(props) {
  return (
    <div>
      <h1>{props.title}</h1>
      <p>{props.description}</p>
    </div>
  );
}

export default Card;

The defaultProps property has traditionally supplied a fallback when a prop is omitted:

// Card.jsx
function Card(props) {
  // ...
}

Card.defaultProps = {
  title: 'Default title',
  description: 'Desc',
};

export default Card;

Modern JavaScript lets you destructure the props object and assign defaults directly in the function argument instead:

// Card.jsx
function Card({title = "Default title", description= "Desc"}) {
  return (
    <div>
      <h1>{title}</h1>
      <p>{description}</p>
    </div>
  )
}

export default Card;

This form is preferable because modern browsers can read it without extra transformation. defaultProps requires transformation, since JSX (JavaScript XML) isn't supported out of the box, which could affect the performance of an application that leans heavily on defaultProps.

Type checking: move from propTypes to TypeScript

The propTypes property lets you declare the expected type for each prop — string, number, object and so on — and whether it is required. If a component receives the wrong type or misses a required prop, React throws an error.

// Card.jsx
import { PropTypes } from "prop-types";

function Card(props) {
  // ...
}

Card.propTypes = {
  title: PropTypes.string.isRequired,
  description: PropTypes.string,
};

export default Card;

TypeScript, a language built on top of JavaScript with static type-checking added, has since become the go-to solution for type safety. Its type system is more powerful, catches more potential bugs and improves the development experience — which is why it is the better recommendation today.

// Card.tsx
interface CardProps {
  title: string,
  description?: string,
}

export default function Card(props: CardProps) {
  // ...
}

Write components as functions, not classes

Class components are built with JavaScript classes. They offer a more object-oriented structure plus extra capabilities, such as the this keyword and lifecycle methods.

// Card.jsx
class Card extends React.Component {
  render() {
    return (
      <div>
        <h1>{this.props.title}</h1>
        <p>{this.props.description}</p>
      </div>
    )
  }
}

export default Card;

Classes are harder for beginners to grasp, though, and this can get very confusing. Functions are the better shape for a component:

// Card.jsx
function Card(props) {
  return (
    <div>
      <h1>{props.title}</h1>
      <p>{props.description}</p>
    </div>
  )
}

export default Card;

A function component is simply a JavaScript function that returns JSX. It is easier to read, and because it lacks features like this and lifecycle methods, it is more performant than a class component. Functions also open the door to hooks: React Hooks let you use state and other React features without writing a class, making code more readable, maintainable and reusable.

The React import you no longer need

Since React 17 shipped in 2020, there is no need to import React at the top of a file when you create a component.

import React from 'react'; // Not needed!
export default function Card() {}

Before React 17 the import was mandatory because the JSX transformer — the piece that turns JSX into regular JavaScript — called React.createElement, which only worked when React was imported. A newer transformer converts JSX without createElement. You still need the import for hooks, fragments and any other functions or components you pull from the library:

import { useState } from 'react';

export default function Card() {
  const [count, setCount] = useState(0);
  // ...
}

Learning is a journey, not a switch

"Mistake" may be too harsh a word, since several of these better practices only emerged later. Even so, the older approaches still show up actively in projects and tutorials.

There were probably more than five missteps in that first project. Picking up any new tool is a learning journey rather than flipping a switch — but these are the lessons that stuck.