Crafting Flexible UIs with Generic and Domain Components in React

Listen to this Post

Read the full article here: https://buff.ly/teKnDlM

You Should Know:

When working with React, organizing components into Generic and Domain categories can significantly improve code modularity and maintainability. Here’s how you can implement this approach effectively:

1. Generic Components

Generic components are reusable and independent of business logic. Examples include buttons, modals, and input fields. Here’s an example of a generic button component:

[jsx]
import React from ‘react’;

const GenericButton = ({ onClick, children }) => {
return (

);
};

export default GenericButton;
[/jsx]

2. Domain Components

Domain components are tied to specific business logic. For example, a `ProductCard` component for an e-commerce app:

[jsx]
import React from ‘react’;
import GenericButton from ‘./GenericButton’;

const ProductCard = ({ product, onAddToCart }) => {
return (

{product.name}

{product.description}

Price: ${product.price}
onAddToCart(product)}>Add to Cart

);
};

export default ProductCard;
[/jsx]

3. Folder Structure

Organize your project structure to separate generic and domain components:

src/
β”‚
β”œβ”€β”€ components/
β”‚ β”œβ”€β”€ generic/
β”‚ β”‚ β”œβ”€β”€ GenericButton.jsx
β”‚ β”‚ β”œβ”€β”€ GenericModal.jsx
β”‚ β”‚ └── GenericInput.jsx
β”‚ └── domain/
β”‚ β”œβ”€β”€ ProductCard.jsx
β”‚ └── UserProfile.jsx
└── App.js

4. Refactoring for Scalability

When business requirements change, refactor domain components without affecting generic ones. For example, updating the `ProductCard` to include a discount badge:

[jsx]
const ProductCard = ({ product, onAddToCart }) => {
return (

{product.name}

{product.discount && {product.discount}% OFF!}
{product.description}

Price: ${product.price}
onAddToCart(product)}>Add to Cart

);
};
[/jsx]

5. Testing

Use testing libraries like Jest and React Testing Library to ensure your components work as expected:

npm install --save-dev @testing-library/react @testing-library/jest-dom

Example test for `GenericButton`:

[jsx]
import { render, screen, fireEvent } from ‘@testing-library/react’;
import GenericButton from ‘./GenericButton’;

test(‘renders GenericButton and handles click’, () => {
const handleClick = jest.fn();
render(Click Me);
const buttonElement = screen.getByText(/Click Me/i);
fireEvent.click(buttonElement);
expect(handleClick).toHaveBeenCalledTimes(1);
});
[/jsx]

What Undercode Say:

Organizing React components into Generic and Domain categories is a powerful strategy for building scalable and maintainable applications. By separating reusable logic from business-specific logic, you can adapt to changing requirements more efficiently. This approach also simplifies testing and collaboration among team members. For further reading, check out the full article: https://buff.ly/teKnDlM.

Additionally, here are some Linux and IT-related commands to enhance your development workflow:

  • Linux Commands:
    – `grep “pattern” file.txt` – Search for a pattern in a file.
    – `chmod 755 script.sh` – Make a script executable.
    – `ssh user@hostname` – Connect to a remote server via SSH.

  • Windows Commands:
    – `ipconfig` – Display network configuration.
    – `tasklist` – List all running processes.
    – `sfc /scannow` – Scan and repair system files.

By combining these strategies and tools, you can streamline your development process and build robust applications.

References:

Reported By: Petarivanovv9 Ive – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass βœ…

Join Our Cyber World:

πŸ’¬ Whatsapp | πŸ’¬ TelegramFeatured Image