What are the differences between functional and class components?

Beginner

Answer

Functional Components:

  • Simpler syntax
  • Use React Hooks for state and lifecycle methods
  • Better performance (less overhead)
  • Easier to test and debug
  • Preferred approach in modern React
function MyComponent(props) {
  const [count, setCount] = useState(0);
  
  return <div>{count}</div>;
}

Class Components:

  • More verbose syntax
  • Use this.state and lifecycle methods
  • Have access to this context
  • Legacy approach (still supported)
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
  
  render() {
    return <div>{this.state.count}</div>;
  }
}