React 18 + TypeScript: A Powerful Combination for Frontend Applications

Explore the synergies of React 18 and TypeScript in building robust frontend applications

Posted by Louis Lu on Saturday, February 17, 2024

1. Introduction to React

React, developed by Facebook, is a JavaScript library for building user interfaces. It utilizes a virtual DOM to enhance performance and provides a declarative programming approach for UI construction. React has become one of the most popular tools for building modern web applications.

1.1 Advantages of React

  • High Performance: React boosts performance by using a virtual DOM and provides performance optimization techniques. A prime example is the useMemo hook in React Hooks, which can be used for memoizing computations, preventing unnecessary recalculations. Below is an example using useMemo in a functional component: useMemo recalculates only when someProp changes, avoiding unnecessary computations and rendering.
import React, { useMemo, useState } from 'react';

function MyComponent(props) {
  const [someProp, setSomeProp] = useState(0);

  const memoizedComputations = useMemo(() => {
    // Perform some expensive computations here
    const result = someExpensiveComputation(someProp);
    return result;
  }, [someProp]);

  return (
    <div>
      {memoizedComputations}
      <button onClick={() => setSomeProp(someProp + 1)}>Increment</button>
    </div>
  );
}
  • Ease of Use: React’s declarative programming makes it easy to learn and use.
  • Robust Community: React boasts a large and active community, offering abundant learning resources and support.

2. New Features in React 18

  • Concurrent Mode: Allows React to concurrently render multiple components in the background, enhancing application performance and responsiveness.
  • Automatic Batching Updates: Automatically combines multiple state updates into a single update, reducing rendering times and improving performance.
  • New Hooks API: Introduces new hooks like useDeferredValue and useId to help developers build more complex applications.

2.1 Concurrent Mode

Concurrent Mode is one of the most significant features in React 18. It allows React to concurrently render multiple components in the background, improving application performance and responsiveness.

In React 17, when a component’s state updates, React re-renders the entire component tree. This could lead to performance issues, especially in large applications.

In React 18, Concurrent Mode allows React to:

  • Divide the component tree into multiple parts.
  • Render only the visible component parts when necessary.
  • Asynchronously render the invisible component parts in the background.

Concurrent Mode significantly enhances application performance, particularly in large applications.

Code Example:

function App() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setCount(count => count + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={() => setCount(count => count + 1)}>Increase Count</button>
    </div>
  );
}

In this example, the useEffect hook updates the count every second. In React 17, this would result in re-rendering the entire application with each update. However, in React 18’s Concurrent Mode, the count updates in the background, ensuring a smooth user interface.

2.2 Automatic Batching Updates

Automatic Batching Updates is another important feature in React 18. It automatically combines multiple state updates into a single update, reducing rendering times and improving performance.

In React 17, if a component’s state updates multiple times in a short period, React renders the component for each update. This could lead to performance issues, especially in components with frequent state updates.

In React 18, Automatic Batching Updates automatically combines multiple state updates into a single update. This is achieved by:

  • Collecting state updates in a queue.
  • Performing an update only after a certain time interval or when the queue reaches a certain length.

Automatic Batching Updates significantly improve application performance, especially in components with frequent state updates.

Code Example:

function App() {
  const [count1, setCount1] = useState(0);
  const [count2, setCount2] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setCount1(count1 => count1 + 1);
      setCount2(count2 => count2 + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <div>
      <h1>Count 1: {count1}</h1>
      <h1>Count 2: {count2}</h1>
    </div>
  );
}

In this example, the useEffect hook updates two counts every second. In React 17, this would result in rendering the entire application twice for each update. In React 18, Automatic Batching Updates combine these two updates into one, improving performance.

2.3 New Hooks API

React 18 introduces new hooks such as useDeferredValue and useId to assist developers in building more complex applications.

  • The useDeferredValue hook helps developers delay rendering low-priority state updates.
  • The useId hook helps developers generate a unique ID for each component.

useDeferredValue Hook Example:

function App() {
  const [count, setCount] = useState(0);
  const deferredCount = useDeferredValue(count);

  useEffect(() => {
    const interval = setInterval(() => {
      setCount(count => count + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <div>
      <h1>Count: {deferredCount}</h1>
      <button onClick={() => setCount(count => count + 1)}>Increase Count</button>
    </div>
  );
}

In this example, the useDeferredValue hook delays rendering count updates. This means the count updates are only rendered when the user stops scrolling or input, improving performance.

useId Hook Example:

function App() {
  const [items, setItems] = useState([]);

  const addItem = () => {
    const id = useId();
    setItems(items => [...items, { id, text: `Item ${id}` }]);
  };

  return (
    <div>
      <ul>
        {items.map(item => (
          <li key={item.id}>{item.text}</li>
        ))}
      </ul>
      <button onClick={addItem}>Add an item</button>
    </div>
  );
}

In this example, the useId hook generates a unique ID for each new item. This helps developers track items in the list and ensures each item has a unique identifier.

3. Introduction to TypeScript

TypeScript is a superset of JavaScript that adds type checking and a static type system. This helps developers avoid errors while writing code and enhances code reliability and maintainability.

3.1 Advantages of TypeScript

  • Improved Code Quality: Type checking helps developers avoid errors, improving code reliability and maintainability.
  • Enhanced Development Efficiency: The static type system allows developers to write code faster and reduce debugging time.
  • Better Code Collaboration: Type checking helps developers better understand each other’s code, improving code collaboration efficiency.

4. Combined Advantages of React 18 + TypeScript

4.1 Performance Boost

React 18’s Concurrent Mode enhances application performance, and TypeScript helps developers write more efficient code.

For example, TypeScript helps developers avoid unnecessary type conversions, improving code execution efficiency.

4.2 Enhanced Code Quality

TypeScript’s type checking ensures developers avoid errors, enhancing code reliability and maintainability.

For example, TypeScript helps developers ensure the correct types of props for components, preventing runtime errors.

4.3 Improved Development Efficiency

TypeScript’s static type system helps developers write code faster and reduces debugging time.

For example, TypeScript provides automatic code completion and type-checking error hints, helping developers find and fix errors more quickly.

5. Projects Using React 18 + TypeScript Combination

Many popular projects leverage the combination of React 18 and TypeScript, showcasing the powerful advantages of this combination. Here are detailed insights into a few typical projects, discussing their features and key technologies:

5.1 Microsoft Teams

Features: Microsoft Teams is a platform for collaboration and communication, offering features such as chat, video meetings, and file sharing.

Key Technologies:

  • React 18’s Concurrent Mode enhances team chat performance, ensuring a smooth experience even with a large number of users.
  • TypeScript ensures code reliability and maintainability, enabling rapid development and iteration of new features.
  • Other technologies include WebRTC, Redux, Jest, and more.

5.2 Adobe XD

Features: Adobe XD is a vector graphic design and user experience design tool used for creating websites, mobile apps, game interfaces, and more.

Key Technologies:

  • React 18’s Concurrent Mode allows XD to handle complex designs smoothly, even when using a large number of components.
  • TypeScript ensures the robustness of XD’s code and helps developers improve development efficiency.
  • Other technologies include React Native, GraphQL, Storybook, and more.

5.3 New York Times

Features: The New York Times is a globally renowned news organization, and its website provides news, commentary, videos, and more.

Key Technologies:

  • React 18’s Concurrent Mode improves the loading speed and responsiveness of The New York Times website, providing a better reading experience.
  • TypeScript ensures code quality and maintainability, allowing the website to evolve and update continuously.
  • Other technologies include Next.js, Apollo GraphQL, Jest, and more.

5.4 Netflix

Features: Netflix is a streaming service offering movies, TV shows, and more.

Key Technologies:

  • React 18’s Concurrent Mode allows Netflix to provide a smoother watching experience, even when streaming high-quality videos.
  • TypeScript ensures the reliability and scalability of Netflix’s code, supporting its vast user base and complex feature requirements.
  • Other technologies include GraphQL, Relay, Jest, and more.

These are just a few examples of projects adopting the React 18 + TypeScript combination. The advantages of this combination have been validated by numerous well-known projects, helping developers build frontend applications that are higher in performance, quality, and efficiency.

6. Conclusion

React 18 and TypeScript are powerful tools that help developers build stronger frontend applications. Combining them enhances application performance, code quality, and development efficiency.

7. Learning Resources