Free ATS Friendly Resume Builder Online

Create Your Resume

Resume Builder

Resume Maker

Resume Templates

Resume PDF Download

Create Your Resume is a free online resume builder that helps job seekers create professional, ATS friendly resumes in minutes. Easily build, customize, and download modern resume templates in PDF format.

Our resume maker is designed for freshers and experienced professionals looking to create job-ready resumes. Choose from multiple resume templates, customize sections, and generate ATS optimized resumes online for free.

Create resumes for IT jobs, software developers, freshers, experienced professionals, managers, and students. This free resume builder supports CV creation, resume PDF download, and online resume editing without signup.

Back to React Js
Lesson 33 of 59

Derived State in React: What It Is, How It Works, When to Use It, and When to Avoid It

Derived state is a React concept where component state is calculated from props or other state values instead of being stored independently. While derived state can be useful in specific scenarios, improper usage often leads to bugs, duplicated data, and unnecessary re-renders. Understanding when and how to use derived state helps developers write predictable, maintainable, and performant React applications. This guide explains what derived state is, how to implement it correctly, common mistakes, real-world examples, React patterns, and best practices. It also highlights modern alternatives using hooks to avoid anti-patterns and improve application architecture.

What is Derived State?

Derived state refers to state that is computed from props or other state rather than being the single source of truth itself. Instead of storing raw data, the component derives its value dynamically.

In simple terms: if a value can be calculated, it usually should not be stored.

Why Derived State Exists

Derived state is mainly used to:

  • Transform incoming props
  • Sync state with prop changes
  • Cache expensive computations (rare cases)

For example, filtering or formatting data before displaying it in tools like dynamic form builders is a common derived-state use case.

Basic Example of Derived State (Anti-Pattern)


function UserProfile({ user }) {
  const [name, setName] = useState(user.name);

  useEffect(() => {
    setName(user.name);
  }, [user.name]);

  return <p>{name}</p>;
}

⚠️ This is usually an anti-pattern because:

  • State is duplicated
  • Extra re-renders occur
  • Risk of data going out of sync

Correct Way: Compute Instead of Store


function UserProfile({ user }) {
  const name = user.name;
  return <p>{name}</p>;
}

This keeps a single source of truth and avoids unnecessary state.

Derived State Using Computation

Example: Filtering Data


function ProductList({ products, search }) {
  const filteredProducts = products.filter(product =>
    product.name.toLowerCase().includes(search.toLowerCase())
  );

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

Here, filteredProducts is derived state but not stored in React state.

Derived State with useMemo (Performance Optimization)

When the computation is expensive, use useMemo.


import { useMemo } from "react";

function ProductList({ products, search }) {
  const filteredProducts = useMemo(() => {
    return products.filter(product =>
      product.name.toLowerCase().includes(search.toLowerCase())
    );
  }, [products, search]);

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

This is commonly used in real-world applications that render large datasets, such as previewing content layouts while generating customized documents.

When Derived State is Actually Needed

Derived state is acceptable when:

  • You need to reset internal state when a prop changes
  • You need to track previous prop values
  • You must control animations or transitions

Example: Resetting State on Prop Change


function Form({ initialEmail }) {
  const [email, setEmail] = useState(initialEmail);

  useEffect(() => {
    setEmail(initialEmail);
  }, [initialEmail]);

  return (
    <input
      value={email}
      onChange={e => setEmail(e.target.value)}
    />
  );
}

This is valid derived state because the component intentionally mirrors props.

Class Component Example (getDerivedStateFromProps)


class Example extends React.Component {
  state = { value: null };

  static getDerivedStateFromProps(props, state) {
    if (props.value !== state.value) {
      return { value: props.value };
    }
    return null;
  }

  render() {
    return <p>{this.state.value}</p>;
  }
}

⚠️ This method is rarely recommended in modern React.

Common Mistakes with Derived State

  • Copying props into state unnecessarily
  • Storing computed values instead of computing them
  • Using derived state instead of lifting state up

These mistakes often appear in poorly structured applications and are discussed frequently in frontend architecture guides.

Best Practices & Special Notes

  • Prefer computation over storage
  • Keep a single source of truth
  • Use useMemo for expensive calculations
  • Avoid syncing props to state unless necessary

Testing your understanding with scenario-based questions on interactive quizzes can help avoid derived-state anti-patterns in real projects.

Final Takeaway

Derived state is powerful but dangerous when misused. If a value can be calculated, calculate it. Store state only when the data truly needs to be controlled independently. Mastering derived state is a key step toward writing clean, predictable, and professional React applications.