High Order Components: React.js

High Order Components: React.js

Β·

4 min read

πŸŽ‰πŸ“ Welcome to the world of React and the fascinating realm of Advanced React Concepts! Today, we're going to dive into one such concept that might seem a bit puzzling at first but can truly level up your React game: High Order Components (HOCs) πŸš€. So grab your coding hats, and let's embark on this exciting journey together!

So, why do we need High Order Components anyway? Well, picture this: you're building a React application, and you realize that you have some repeating logic or functionality that you want to share across multiple components. Copy-pasting code is not an option, as it leads to code duplication and maintenance headaches. Here's where HOCs come to the rescue! πŸ¦Έβ€β™‚οΈ

Let's say you have a couple of components that need to fetch data from an API. Instead of writing the same code for data fetching in each component, we can create a HOC called withDataFetching to handle this task for us. πŸ“‘

 const withDataFetching = (WrappedComponent, dataSource) => {
  return class extends React.Component {
    constructor() {
      super();
      this.state = {
        data: [],
        isLoading: true,
        error: null,
      };
    }

    async componentDidMount() {
      try {
        const response = await fetch(dataSource);
        const data = await response.json();
        this.setState({ data, isLoading: false });
      } catch (error) {
        this.setState({ error, isLoading: false });
      }
    }

    render() {
      const { data, isLoading, error } = this.state;

      return (
        <WrappedComponent
          data={data}
          isLoading={isLoading}
          error={error}
          {...this.props}
        />
      );
    }
  };
};

With our withDataFetching HOC in place, we can now effortlessly enhance our components by wrapping them with it. For example, let's imagine we have a UserList component that needs to fetch a list of users:

class UserList extends React.Component {
  render() {
    const { data, isLoading, error } = this.props;

    if (isLoading) {
      return <div>Loading...</div>;
    }

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

    return (
      <ul>
        {data.map((user) => (
          <li key={user.id}>{user.name}</li>
        ))}
      </ul>
    );
  }
}

const UserListWithFetching = withDataFetching(
  UserList,
  'https://api.example.com/users'
);

VoilΓ ! πŸŽ‰ By wrapping our UserList component with withDataFetching, we have transformed it into UserListWithFetching, which now automatically handles the data fetching logic. Our enhanced component receives the data, loading status, and any potential errors as props, making it clean and reusable. Isn't that neat? 😎

Now let's take a moment to compare both approaches: the traditional one, where we'd manually write the data fetching logic in each component, and the HOC approach. With HOCs, we have eliminated the repetitive code and made our components more focused on their core responsibilities. Plus, if we need to make changes or add new functionality to the data fetching logic, we can do it in just one place, the HOC itself, and all the components using it will automatically benefit from the update. πŸš€βœ¨

Real-life use cases for HOCs are plentiful! For instance, you could create a withAuthentication HOC to handle authentication logic, ensuring that only authenticated users can access certain components. Or how about a withWindowDimensions HOC to provide window dimensions as props to components that need them? The possibilities are endless, and HOCs offer a flexible and reusable way to solve these challenges in your React applications. πŸŒŸπŸ”§

So there you have it! High Order Components are like trusty companions that help us enhance and simplify our React components by encapsulating shared logic. They save us from code repetition, improve maintainability, and make our lives as React developers a whole lot easier. Happy coding! πŸš€πŸ”₯

Here's the functional component version of the code using React hooks:

import React, { useState, useEffect } from 'react';

const withDataFetching = (WrappedComponent, dataSource) => {
  return (props) => {
    const [data, setData] = useState([]);
    const [isLoading, setIsLoading] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {
      const fetchData = async () => {
        try {
          const response = await fetch(dataSource);
          const data = await response.json();
          setData(data);
          setIsLoading(false);
        } catch (error) {
          setError(error);
          setIsLoading(false);
        }
      };

      fetchData();
    }, []);

    return (
      <WrappedComponent
        data={data}
        isLoading={isLoading}
        error={error}
        {...props}
      />
    );
  };
};

const UserList = ({ data, isLoading, error }) => {
  if (isLoading) {
    return <div>Loading...</div>;
  }

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

  return (
    <ul>
      {data.map((user) => (
        <li key={user.id}>{user.name}</li>
      ))}
    </ul>
  );
};

const UserListWithFetching = withDataFetching(
  UserList,
  'https://api.example.com/users'
);

export default UserListWithFetching;

Did you find this article valuable?

Support Zeeshan Ashraf by becoming a sponsor. Any amount is appreciated!

Β