In React, you have two main ways to define components: function components and class components. Both serve the same purpose—to create reusable blocks of UI—but they have different approaches and use cases.

Function Components:

  • Defined as simple JavaScript functions that accept props (properties from parent components) as arguments and return JSX (a syntax extension for defining UI elements).
  • Easier to write and understand, especially for simpler components.
  • Don’t have access to lifecycle methods (like componentDidMount) or state directly.
  • Since React 16.8, can use hooks (like useState, useEffect) to handle state and side effects.

Example:

function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}

Class Components:

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help