DEV Community

Cover image for After Reviewing 500+ React Components, Here’s What I Have Learned
Safdar Ali
Safdar Ali

Posted on

After Reviewing 500+ React Components, Here’s What I Have Learned

Over the past few years, I’ve had the privilege of writing and reviewing over 500 React components for various projects, including startups. And through this extensive review process, I’ve uncovered some crucial insights. Whether you're a beginner or an experienced developer, these guidelines will help you write more understandable, maintainable, and efficient React components.

1. Consistent Structure is Key

🛑 Don’t:
A common mistake I’ve seen is the lack of a consistent structure within components. For example, some developers mix state hooks, effect hooks, and custom hooks without a clear order, making the component difficult to read and maintain.

✅ Do:
Always follow a consistent order when structuring your components. Typically, this order works well:

  1. Import statements
  2. Component declaration
  3. State and refs
  4. Effect hooks
  5. Event handlers
  6. Render logic
import React, { useState, useEffect } from 'react';

const MyComponent = () => {
  // State hooks
  const [data, setData] = useState(null);

  // Effect hooks
  useEffect(() => {
    fetchData();
  }, []);

  // Event handlers
  const fetchData = async () => {
    const result = await fetch('https://api.example.com/data');
    setData(await result.json());
  };

  return (
    <div>
      {data ? <p>{data}</p> : <p>Loading...</p>}
    </div>
  );
};

export default MyComponent;

Enter fullscreen mode Exit fullscreen mode

2. Keep Components Small and Focused

🛑 Don’t:
Avoid creating monolithic components that handle too many responsibilities. Large components are hard to read, test, and maintain.

✅ Do:
Break down your components into smaller, reusable pieces. A good rule of thumb is the single responsibility principle: each component should do one thing and do it well.

const UserProfile = ({ user }) => {
  return (
    <div>
      <UserAvatar avatarUrl={user.avatarUrl} />
      <UserDetails name={user.name} email={user.email} />
    </div>
  );
};

const UserAvatar = ({ avatarUrl }) => <img src={avatarUrl} alt="User Avatar" />;
const UserDetails = ({ name, email }) => (
  <div>
    <p>{name}</p>
    <p>{email}</p>
  </div>
);

Enter fullscreen mode Exit fullscreen mode

3. Use PropTypes for Type Checking

🛑 Don’t:
Ignoring prop types can lead to bugs and make your components less predictable.

✅ Do:
Use PropTypes to document the expected types of props and catch bugs early.

import PropTypes from 'prop-types';

const UserProfile = ({ user }) => {
  return (
    <div>
      <UserAvatar avatarUrl={user.avatarUrl} />
      <UserDetails name={user.name} email={user.email} />
    </div>
  );
};

UserProfile.propTypes = {
  user: PropTypes.shape({
    avatarUrl: PropTypes.string.isRequired,
    name: PropTypes.string.isRequired,
    email: PropTypes.string.isRequired
  }).isRequired
};

Enter fullscreen mode Exit fullscreen mode

4. Optimize Performance with Memoization

🛑 Don’t:
Re-rendering expensive components unnecessarily can lead to performance issues.

✅ Do:
Use React.memo and useMemo to optimize performance by memoizing components and values.

import React, { memo, useMemo } from 'react';

const ExpensiveComponent = ({ data }) => {
  const computedValue = useMemo(() => computeExpensiveValue(data), [data]);

  return <div>{computedValue}</div>;
};

export default memo(ExpensiveComponent);

Enter fullscreen mode Exit fullscreen mode

5. Write Tests for Your Components

🛑 Don’t:
Neglecting to test your components can lead to regressions and hard-to-find bugs.

✅ Do:
Write unit tests for your components to ensure they work as expected.

import { render, screen } from '@testing-library/react';
import UserProfile from './UserProfile';

test('renders user profile', () => {
  const user = { avatarUrl: 'avatar.png', name: 'John Doe', email: 'john@example.com' };
  render(<UserProfile user={user} />);

  expect(screen.getByText('John Doe')).toBeInTheDocument();
  expect(screen.getByText('john@example.com')).toBeInTheDocument();
});

Enter fullscreen mode Exit fullscreen mode

Conclusion
Reviewing over 500 React components has given me a unique perspective on what works and what doesn’t. By following these guidelines—maintaining a consistent structure, keeping components small, using PropTypes, optimizing performance, and writing tests—you can ensure your React components are robust, maintainable, and performant. Implement these practices in your projects and watch your code quality improve significantly.

That's all for today.

And also, share your favourite web dev resources to help the beginners here!

Connect with me:@ LinkedIn and checkout my Portfolio.

Explore my YouTube Channel! If you find it useful.

Please give my GitHub Projects a star ⭐️

Thanks for 26959! 🤗

Top comments (3)

Collapse
 
joelquix profile image
Joel

Who did the banner illustration? It's really nice.

Collapse
 
safdarali profile image
Safdar Ali

If you found my content helpful or interesting, and you’d like to show your appreciation, why not buy me a coffee? It’s a small gesture that goes a long way in helping me keep creating more content for you.

Just click the button below to support:

Buy Me A Coffee

Collapse
 
safdarali profile image
Safdar Ali

Subscribe to my YouTube Channel if you find it helpful! Subscribing is free, and it will motivate me to stay active here. 😊