- React Tutorial
- React Home
- React Setup
- React Introduction
- React ES6
- React Render HTML
- React JSX
- React Components
- React Class
- React Props
- React Events
- React Conditional
- React Lists
- React Forms
- React Router
- React Memo
- React CSS Styling
- React Hooks
- What is a Hook?
- React useState
- React useEffect
- React useContext
- React useRef
- React useReducer
- React useCallback
- React useMemo
- React Custom Hooks
React Components
- In React, a component is the fundamental building block of your application. Think of them as custom, supercharged HTML elements.
- It is a reusable and self-contained piece of code that manages its own logic and appearance.
- Components represent specific parts of the user interface (UI), ranging from a simple button to a complex navigation bar or a full user profile page.
Benefits of Components
- Reusability: You can write a "Button" component once and use it dozens of times across your app, ensuring consistency.
- Modularity: They allow you to break down a massive UI into smaller, manageable pieces that are easier to debug.
- Maintainability: Because logic is encapsulated, fixing a bug in one component won't unexpectedly break another part of your site.
- Separation of Concerns: Each component handles its own presentation and logic, making the codebase much cleaner for teams to work on.
Creating React Components
In the modern React ecosystem, components are essentially JavaScript functions that return JSX (JavaScript XML). Let's look at the two primary ways components have been defined.
Functional Components
Functional components are the modern standard in React. They are simpler to write, easier to test, and use "Hooks" to manage complex logic.
import React from 'react';
// A simple functional component
const Greeting = () => {
return (
Hello, Developer!
Welcome to the world of React.
);
};
export default Greeting;
Greeting, not greeting). React treats lowercase tags as standard HTML elements like <div> or <span>.
Class Components
Before React Hooks were introduced in version 16.8, class components were the only way to manage state and lifecycle methods. While you will still see them in older "legacy" codebases, functional components are now preferred for new projects.
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return (
Current Count: {this.state.count}
);
}
}
Props
Props (short for "properties") are the way we pass data from a parent component down to a child component. They make your components dynamic and flexible.
import React from 'react';
// The component receives 'props' as an object
const WelcomeMessage = (props) => {
return (
Welcome back, {props.name}!
Status: {props.isAdmin ? 'Administrator' : 'User'}
);
};
// Usage:
Lifecycle Methods
Lifecycle methods allow you to run code at specific moments, such as when a component first appears on the screen (mounting) or when it is about to be removed (unmounting). In class components, this looks like this:
import React, { Component } from 'react';
class LifecycleDemo extends Component {
componentDidMount() {
console.log("The component is now visible on the screen!");
// Great place for API calls
}
componentWillUnmount() {
console.log("The component is about to be destroyed.");
// Great place to clean up timers or subscriptions
}
render() {
return Check your console logs!;
}
}
useEffect. It's much more concise than managing multiple class methods.
Summary
React components are the cornerstone of building modern, scalable user interfaces. By mastering the transition from static HTML to dynamic, reusable components, you gain the ability to build complex applications with ease. Remember to keep your components small, use functional components for new work, and treat props as read-only data passed from above.