In modern React, a component is a JavaScript function that accepts arbitrary inputs (called props) and returns a React element (written in JSX) describing how a section of the user interface should appear.
Components allow you to split the UI into independent, reusable pieces, and think about each piece in isolation.
In React, component names must always start with an uppercase letter (e.g., CourseCard, Header, UserProfile).
If an element starts with a lowercase letter (e.g., <div>, <span>, <button>), React treats it as a built-in standard HTML tag.
If an element starts with an uppercase letter (e.g., <CourseCard />), React recognizes it as a custom component and executes the corresponding JavaScript function.
1// ❌ Treated as an unknown HTML tag; will not invoke the function
11// Using <CustomButton /> invokes your custom function
3. Pure Functions and the Declarative Contract
React components are modeled after pure functions in computer science:
Same Inputs -> Same Output: Given the same props and state, a component should always return the exact same JSX.
No Side Effects During Render: A component function should simply describe what the UI should look like. It must not mutate external variables, modify the DOM directly, or start network requests inside the component body during rendering.
1// ❌ Impure Component: Mutates an external variable during rendering!
2let guestCount =0;
3
4exportfunctionCup(){
5 guestCount = guestCount +1;// Side effect during render!
6return<h2>Tea cup for guest #{guestCount}</h2>;
7}
8
9// ✅ Pure Component: Operates strictly on inputs (props)
10exportfunctionCup({ guestNumber }){
11return<h2>Tea cup for guest #{guestNumber}</h2>;
12}
4. Class Components vs Modern Functional Components
Prior to React 16.8, complex components with state and lifecycle methods had to be written as ES6 Classes inheriting from React.Component:
1// Legacy Class Component (Older Codebases)
2classWelcomeextendsReact.Component{
3render(){
4return<h1>Hello,{this.props.name}</h1>;
5}
6}
Today, the entire React ecosystem has standardized on Functional Components with React Hooks:
Simpler Code: No this binding headaches, constructor methods, or class boilerplate.
Smaller Bundle Sizes: Functions minify and compress far better than JavaScript ES6 classes.
Code Sharing via Custom Hooks: Business and state logic can be extracted into reusable custom hooks without convoluted Higher-Order Component (HOC) or Render Prop patterns.
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
What rule must the naming of a React functional component strictly follow?
Question 2
What is the primary characteristic of a pure React component?
Question 3
What version of React introduced Hooks, transitioning the ecosystem from Class components to Functional components?
Question 4
Why are modern functional components preferred over legacy class components?
Question 5
What does a functional component return?
Next Lesson
Component Props and Read-Only Data Flow
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.