DEV Community

Cover image for Common Mistakes That Cause Unhandled Runtime Errors in React Typescript
Ashfiquzzaman Sajal
Ashfiquzzaman Sajal

Posted on

Common Mistakes That Cause Unhandled Runtime Errors in React Typescript

Working with React and TypeScript (TSX) offers many benefits, including type safety and better tooling. However, developers can still run into unhandled runtime errors. These errors can be frustrating and time-consuming to debug. Here are some common mistakes that cause unhandled runtime errors in React TSX and how to avoid them.

1. Accessing Undefined or Null Values

Mistake:

Attempting to access properties on an undefined or null value is a frequent cause of runtime errors.

Example:

const UserProfile: React.FC<{ user: User | undefined }> = ({ user }) => {
  return <div>{user.name}</div>;
};
Enter fullscreen mode Exit fullscreen mode

If user is undefined, accessing user.name will throw an error.

Solution:

Use optional chaining or conditional rendering to ensure the value is defined.

const UserProfile: React.FC<{ user: User | undefined }> = ({ user }) => {
  return <div>{user?.name ?? 'Guest'}</div>;
};
Enter fullscreen mode Exit fullscreen mode

2. Incorrectly Using Array Methods

Mistake:

Using array methods like map, filter, or reduce on a variable that is not an array can lead to errors.

Example:

const ItemList: React.FC<{ items: Item[] | undefined }> = ({ items }) => {
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
};
Enter fullscreen mode Exit fullscreen mode

If items is undefined, items.map will throw an error.

Solution:

Provide a default value to ensure the variable is always an array.

const ItemList: React.FC<{ items: Item[] | undefined }> = ({ items = [] }) => {
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
};
Enter fullscreen mode Exit fullscreen mode

3. Failing to Handle Promises Correctly

Mistake:

Not properly handling promises, especially in asynchronous operations within components, can lead to runtime errors.

Example:

const DataFetchingComponent: React.FC = () => {
  const [data, setData] = React.useState<Data | null>(null);

  React.useEffect(() => {
    fetchData().then(response => setData(response.data));
  }, []);

  return <div>{data.name}</div>;
};
Enter fullscreen mode Exit fullscreen mode

If fetchData fails, data will be null, and accessing data.name will throw an error.

Solution:

Handle the loading and error states properly.

const DataFetchingComponent: React.FC = () => {
  const [data, setData] = React.useState<Data | null>(null);
  const [loading, setLoading] = React.useState(true);
  const [error, setError] = React.useState<Error | null>(null);

  React.useEffect(() => {
    fetchData()
      .then(response => setData(response.data))
      .catch(error => setError(error))
      .finally(() => setLoading(false));
  }, []);

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return <div>{data?.name}</div>;
};
Enter fullscreen mode Exit fullscreen mode

4. Mismanaging Component State

Mistake:

Not initializing state properly or updating state in a way that causes inconsistencies can lead to errors.

Example:

const Counter: React.FC = () => {
  const [count, setCount] = React.useState<number>();

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

If count is undefined, count + 1 will throw an error.

Solution:

Initialize state with a valid value.

const Counter: React.FC = () => {
  const [count, setCount] = React.useState<number>(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

5. Ignoring TypeScript Warnings and Errors

Mistake:

Ignoring TypeScript's type warnings and errors can lead to unexpected runtime errors.

Example:

const Greeting: React.FC<{ name: string }> = ({ name }) => {
  return <div>Hello, {name.toUpperCase()}</div>;
};
Enter fullscreen mode Exit fullscreen mode

If name is optional but not treated as such, this can lead to runtime errors when name is undefined.

Solution:

Pay attention to TypeScript warnings and enforce strict type checking.

const Greeting: React.FC<{ name?: string }> = ({ name = 'Guest' }) => {
  return <div>Hello, {name.toUpperCase()}</div>;
};
Enter fullscreen mode Exit fullscreen mode

6. Not Handling Default Props Correctly

Mistake:

Failing to provide default values for optional props can cause errors when those props are accessed without being passed.

Example:

const WelcomeMessage: React.FC<{ message?: string }> = ({ message }) => {
  return <div>{message.toUpperCase()}</div>;
};
Enter fullscreen mode Exit fullscreen mode

If message is undefined, message.toUpperCase() will throw an error.

Solution:

Provide default values for optional props.

const WelcomeMessage: React.FC<{ message?: string }> = ({ message = 'Welcome!' }) => {
  return <div>{message.toUpperCase()}</div>;
};
Enter fullscreen mode Exit fullscreen mode

Conclusion

Unhandled runtime errors can be a significant hindrance in developing robust React applications with TypeScript. By being aware of these common mistakes and following best practices, you can avoid many of these pitfalls. Ensure that you handle undefined values, manage component state correctly, and pay attention to TypeScript warnings. Doing so will lead to more reliable and maintainable code.

Top comments (0)