DEV Community

Parth Raval
Parth Raval

Posted on

10 Frontend Principles That Work in Any Framework

Frontend development evolves rapidly, with new frameworks and libraries emerging frequently. However, some core principles remain timeless and apply across React, Angular, Vue, Svelte, or even vanilla JavaScript.

Whether you're a beginner or an experienced developer, mastering these principles will make you a better frontend engineer. Let’s dive in!

1. Write Semantic HTML

Why? Improves accessibility, SEO, and maintainability.

✅ Do this:

<header>
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </nav>
</header>
<main>
  <article>
    <h1>Article Title</h1>
    <p>Content goes here...</p>
  </article>
</main>
<footer>© 2024</footer>
Enter fullscreen mode Exit fullscreen mode

❌ Not this:

<div id="header">
  <div class="nav">
    <div><a href="/">Home</a></div>
    <div><a href="/about">About</a></div>
  </div>
</div>
<div class="main-content">
  <div class="article">
    <span class="title">Article Title</span>
    <span>Content goes here...</span>
  </div>
</div>
<div id="footer">© 2024</div>
Enter fullscreen mode Exit fullscreen mode

2. Keep Your CSS Modular & Scalable

Why? Avoids specificity wars and makes styling maintainable.

✅ Use BEM (Block-Element-Modifier) or CSS-in-JS:

/* BEM Example */
.button { /* Block */ }
.button--primary { /* Modifier */ }
.button__icon { /* Element */ }
Enter fullscreen mode Exit fullscreen mode

✅ CSS Modules (React Example):

import styles from './Button.module.css';

function Button() {
  return <button className={styles.primary}>Click Me</button>;
}
Enter fullscreen mode Exit fullscreen mode

3. Optimize Performance Early

Why? Users expect fast-loading apps.

Key Optimizations:

  • Lazy-load components/images
  • Minify & bundle code (Webpack, Vite, Rollup)
  • Use debounce/throttle for expensive events

✅ Lazy Loading in React:

const LazyComponent = React.lazy(() => import('./HeavyComponent'));

function App() {
  return (
    <Suspense fallback={<Spinner />}>
      <LazyComponent />
    </Suspense>
  );
}
Enter fullscreen mode Exit fullscreen mode

4. State Management Should Be Predictable

Why? Avoid spaghetti state logic.

✅ Use React Context, Zustand, or Redux (if needed):

// Zustand Example
import { create } from 'zustand';

const useStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
}));

function Counter() {
  const { count, increment } = useStore();
  return <button onClick={increment}>{count}</button>;
}
Enter fullscreen mode Exit fullscreen mode

5. Write Reusable Components

Why? Reduces duplication and improves consistency.

✅ Generic Button Component (React/Vue/Svelte):

function Button({ variant = 'primary', children, onClick }) {
  return (
    <button className={`btn btn-${variant}`} onClick={onClick}>
      {children}
    </button>
  );
}

// Usage
<Button variant="secondary">Submit</Button>
Enter fullscreen mode Exit fullscreen mode

6. Follow Mobile-First Design

Why? Over 60% of web traffic is mobile.

✅ CSS Media Queries:

/* Mobile-first approach */
.container { padding: 1rem; }

@media (min-width: 768px) {
  .container { padding: 2rem; }
}
Enter fullscreen mode Exit fullscreen mode

7. Use Proper Error Handling

Why? Prevents app crashes and improves UX.

✅ React Error Boundary:

class ErrorBoundary extends React.Component {
  state = { hasError: false };

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  render() {
    if (this.state.hasError) {
      return <FallbackUI />;
    }
    return this.props.children;
  }
}

// Usage
<ErrorBoundary>
  <BuggyComponent />
</ErrorBoundary>
Enter fullscreen mode Exit fullscreen mode

8. Secure Your Frontend

Why? Prevent XSS, CSRF, and data leaks.

✅ Sanitize User Inputs (DOMPurify for React):

import DOMPurify from 'dompurify';

function SafeHTML({ html }) {
  const clean = DOMPurify.sanitize(html);
  return <div dangerouslySetInnerHTML={{ __html: clean }} />;
}
Enter fullscreen mode Exit fullscreen mode

9. Write Unit & Integration Tests

Why? Catch bugs before production.

✅ Jest + Testing Library Example:

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

test('Button click works', () => {
  const handleClick = jest.fn();
  render(<Button onClick={handleClick}>Click Me</Button>);
  fireEvent.click(screen.getByText('Click Me'));
  expect(handleClick).toHaveBeenCalled();
});
Enter fullscreen mode Exit fullscreen mode

10. Document Your Code

Why? Helps your team (and future you).

✅ JSDoc Example:

/**
 * Renders a button with customizable variants.
 * @param {Object} props - Component props.
 * @param {'primary' | 'secondary'} props.variant - Button style.
 * @param {ReactNode} props.children - Button content.
 * @param {Function} props.onClick - Click handler.
 */
function Button({ variant, children, onClick }) {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)