Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What are Pure Components and when should you use them? Pure Components क्या हैं और कब use करें?

Answer
// Pure Component - automatically does shallow comparison of props/state
class PureCounter extends React.PureComponent {
  render() {
    return <h1>Count: {this.props.count}</h1>;
  }
}

// Equivalent functional component with React.memo
const PureCounter = React.memo(({ count }) => {
  return <h1>Count: {count}</h1>;
});

// When to use Pure Components:
// 1. Heavy rendering components
// 2. When props/state rarely change
// 3. Performance optimization

// Custom comparison
const Component = React.memo(
  ({ data }) => <div>{data.name}</div>,
  (prevProps, nextProps) => {
    return prevProps.data.id === nextProps.data.id;
  }
);
Pure Component:
- Automatically shallow comparison करता है
- Performance improve करता है
- PureComponent use करो या React.memo

Class:
class MyComponent extends React.PureComponent {}

Functional:
const Component = React.memo(() => {});

Was this answer clear?