Component and PureComponent.
Function components with Hooks are now the recommended way to write React components. However, class components are still fully supported.
Component
Base class for React components when defined using ES6 classes.Constructor
The initial props passed to the component.
The context object (legacy context API).
The updater object injected by the renderer. In most cases, you won’t access this directly.
Instance Properties
this.props
The props object passed to the component. Props are inputs from parent components.this.state
The state object containing component-specific data. State is private to the component.this.refs
An object containing refs to child elements. This is frozen in development mode.this.context
The context value from the nearest matching Provider above this component in the tree.Instance Methods
setState()
Schedule an update to the component’s state object. This is the primary method for triggering UI updates.An object containing the subset of state to update, or a function that returns such an object.When a function is provided, it receives the current state and props as arguments and should return an object to merge into the state.
An optional callback that will be executed after the state is updated and the component is re-rendered.
setState()is asynchronous. The state may not be updated immediately.- Multiple
setState()calls may be batched together for performance. - Never mutate
this.statedirectly. Always usesetState(). - When new state depends on previous state, use the function form to avoid race conditions.
ErrorifpartialStateis not an object, function, or null.
forceUpdate()
Force the component to re-render. This skipsshouldComponentUpdate() but still calls lifecycle methods.
An optional callback that will be executed after the component is re-rendered.
- Bypasses
shouldComponentUpdate() - Calls
componentWillUpdate()andcomponentDidUpdate()(orUNSAFE_componentWillUpdate()in older versions) - Triggers a re-render of the component
Lifecycle Methods
Class components have access to several lifecycle methods:Mounting
constructor(props)- Initialize state and bind methodsstatic getDerivedStateFromProps(props, state)- Sync state to props changesrender()- Return the component’s JSX (required)componentDidMount()- Run side effects after component is added to the DOM
Updating
static getDerivedStateFromProps(props, state)- Sync state to props changesshouldComponentUpdate(nextProps, nextState)- Optimize by skipping unnecessary rendersrender()- Return updated JSXgetSnapshotBeforeUpdate(prevProps, prevState)- Capture DOM information before updatecomponentDidUpdate(prevProps, prevState, snapshot)- Run side effects after update
Unmounting
componentWillUnmount()- Cleanup before component is removed
Error Handling
static getDerivedStateFromError(error)- Update state when a child component throwscomponentDidCatch(error, info)- Log error information
Deprecated Methods
PureComponent
A variant ofComponent that implements shouldComponentUpdate() with a shallow prop and state comparison.
Constructor
Component.
How it Works
PureComponent is identical to Component except that it implements shouldComponentUpdate() with a shallow comparison of props and state:
When to Use PureComponent
UsePureComponent when:
- Your component renders the same output given the same props and state
- Props and state contain only primitive values or immutable objects
- You want automatic performance optimization without writing custom comparison logic
PureComponent when:
- Props or state contain mutable objects (the shallow comparison won’t detect changes)
- Props contain functions or objects created inline in the parent render (they’ll always be different)
- You need custom comparison logic (use
ComponentwithshouldComponentUpdate()instead)
Example
Common Pitfalls
Comparison: Component vs PureComponent
| Feature | Component | PureComponent |
|---|---|---|
| shouldComponentUpdate | Not implemented (always re-renders) | Shallow comparison of props and state |
| Performance | May re-render unnecessarily | Optimized, skips unnecessary re-renders |
| When to use | When you need custom comparison or deal with mutable data | When props and state are immutable |
| Memory usage | Lower | Slightly higher (stores previous props/state) |