Elements and components are fundamental React concepts, but they have different purposes 🎯:
Concept | Element | Component |
---|---|---|
What it is | Object describing UI | Function or class |
Creation | React.createElement() or JSX | Function/class |
Mutability | Immutable | Can have state |
Returns | React element | React element |
Key difference:
In React, elements and components are different but related concepts. Understanding their differences is critical for effective development 🚀.
A React element is a simple object describing what we want to see on screen. Elements are immutable and lightweight 🪶.
// React elements (all of these are objects!)
const element1 = <h1>Hello, world! 👋</h1>;
const element2 = <div className="container">Container 📦</div>;
const element3 = <button onClick={handleClick}>Button 🖱️</button>;
// The same as objects (what React sees internally)
const elementObj = {
type: 'h1',
props: {
children: 'Hello, world! 👋'
}
};
A component is a function or class that accepts props and returns React element(s) 🔄.
// Functional component
function Welcome({ name }) {
// Returns an element
return <h1>Hello, {name}! 👋</h1>;
}
// Class component
class Goodbye extends Component {
render() {
// Returns an element
return <h1>Goodbye, {this.props.name}! 👋</h1>;
}
}
Elements are simple objects:
// Element - plain object
const element = {
type: 'button',
props: {
className: 'btn',
onClick: handleClick,
children: 'Click me! ✨'
}
};
// Cannot be changed - only created anew
Components are functions or classes:
// Component - function or class
function Button({ text, onClick }) {
// Can have logic, state, effects
return <button onClick={onClick}>{text} ✨</button>;
}
// Can be reused with different props
Elements are created directly:
// Creating elements
const title = <h1>Title 📰</h1>;
const paragraph = <p>Paragraph 📝</p>;
const button = <button>Button 🖱️</button>;
Components are created by calling functions or classes:
// Using components
const welcome = <Welcome name="Alexander" />;
const goodbye = <Goodbye name="Alexander" />;
const customButton = <Button text="Save 💾" onClick={save} />;
Elements are immutable:
// Element cannot be changed
const element = <div>Hello 👋</div>;
// element.props.children = "Goodbye"; // ❌ Doesn't work!
// Instead, create a new element
const newElement = <div>Goodbye 👋</div>;
Components can have state:
// Component with state
function Counter() {
const [count, setCount] = useState(0); // ✅ Can change
return (
<div>
<p>Counter: {count} 🔢</p>
<button onClick={() => setCount(count + 1)}>
Increase ➕
</button>
</div>
);
}
Elements are not reusable:
// Each element is unique
const header1 = <h1>Header 1 📌</h1>;
const header2 = <h1>Header 2 📌</h1>;
// No way to "reuse" an element
Components are designed for reusability:
// Component can be used multiple times
function Card({ title, content }) {
return (
<div className="card">
<h2>{title} 📇</h2>
<p>{content} 📝</p>
</div>
);
}
// Reusing the component
<Card title="Card 1" content="Content 1" />
<Card title="Card 2" content="Content 2" />
<Card title="Card 3" content="Content 3" />
Components create elements that React renders to the DOM 🔄:
// 1. Component accepts props
function UserProfile({ user }) {
// 2. Component returns elements
return (
<div className="user-profile">
<img src={user.avatar} alt={user.name} />
<h1>{user.name} 👤</h1>
<p>{user.email} 📧</p>
</div>
);
}
// 3. Using component creates element
const element = <UserProfile user={userData} />;
// 4. React transforms element to DOM
// (actually more complex due to Virtual DOM)
// Step 1: JSX
const app = <Welcome name="Alexander" />;
// Step 2: Transpiled to React.createElement
const appElement = React.createElement(Welcome, { name: "Alexander" });
// Step 3: React calls Welcome component
// Step 4: Welcome returns <h1>Hello, Alexander!</h1> element
// Step 5: React updates DOM
For simple, static parts of the interface:
// Simple elements
const header = <h1>Welcome! 🎉</h1>;
const footer = <footer>© 2023 My Site 🌐</footer>;
function App() {
return (
<div>
{header}
<main>Main content 📚</main>
{footer}
</div>
);
}
For reusable or complex parts:
// Component for reusability
function Button({ type, text, onClick }) {
const buttonTypes = {
primary: 'btn-primary 🟦',
secondary: 'btn-secondary 🟩',
danger: 'btn-danger 🟥'
};
return (
<button
className={buttonTypes[type]}
onClick={onClick}
>
{text} {type === 'primary' ? '✨' : ''}
</button>
);
}
// Using the component
<Button type="primary" text="Save 💾" onClick={save} />
<Button type="secondary" text="Cancel ❌" onClick={cancel} />
<Button type="danger" text="Delete 🗑️" onClick={delete} />
Element:
// Simple element
const simpleButton = <button>Click me! 👆</button>;
Component:
// Reusable component
function SmartButton({ onClick, children, disabled = false }) {
return (
<button
onClick={onClick}
disabled={disabled}
style={{
opacity: disabled ? 0.5 : 1,
cursor: disabled ? 'not-allowed' : 'pointer'
}}
>
{children} {disabled ? '🔒' : '🔓'}
</button>
);
}
// Usage
<SmartButton onClick={handleClick}>Save 💾</SmartButton>
<SmartButton disabled>Unavailable 🚫</SmartButton>
// Elements
const title = <h1>Todo List 📋</h1>;
const item1 = <li>Buy bread 🍞</li>;
const item2 = <li>Call doctor 📞</li>;
// Components
function TodoList({ todos }) {
return (
<div>
<h1>Todo List 📋</h1>
<ul>
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} />
))}
</ul>
</div>
);
}
function TodoItem({ todo }) {
const [completed, setCompleted] = useState(todo.completed);
return (
<li
style={{
textDecoration: completed ? 'line-through' : 'none'
}}
onClick={() => setCompleted(!completed)}
>
{todo.text} {completed ? '✅' : '⏳'}
</li>
);
}
Elements and components are fundamental React concepts with different purposes 🎯:
✅ Elements:
✅ Components:
Key points:
Understanding the difference between elements and components will help you write more efficient and maintainable code in React applications 🚀
Want more articles for interview preparation? Subscribe to EasyAdvice, bookmark the site, and improve every day 💪