1. React Components
1.1 Definition
1.2 Types of Components
1.2.1 Functional Components
1.2.2 Class Components
1.3 Features of Components
1.4 Component Structure
1.5 Creating and Exporting Components
1.6 Importing Components
1.7 Nested Components
1.8 Rendering Components
1.9 Advantages of Components
2. React State
2.1 Definition
2.2 Characteristics of State
2.3 Declaring State in Class Components (this.state)
2.4 Updating State using setState()
2.5 Using State in Functional Components (useState Hook)
2.6 Event Handling and State Change
2.7 Multiple State Variables
2.8 State Lifecycle
2.9 Rules for Using State
2.10 Advantages of Using State
3. React Props
3.1 Definition
3.2 Characteristics of Props
3.3 Passing Data using Props
3.4 Receiving Props in Child Components
3.5 Default Props
3.6 PropTypes Validation
3.7 Props vs State
3.8 Nested Props
3.9 Reusability with Props
3.10 Advantages of Props
4. Combined Example (Integration)
4.1 Using Components with State and Props
4.2 Parent-Child Communication
4.3 Updating State and Passing via Props
4.4 Dynamic Rendering with State and Props
React Components – Formal Definition
A React Component is a reusable, independent block of code that defines the structure (HTML), behavior (JavaScript), and presentation (CSS) of a part of the user interface.
Each component in React returns a piece of JSX (JavaScript XML) that represents how a section of the UI should appear. Components can be functional or class-based, and they help in dividing a complex UI into smaller, manageable parts.
Example:
In simple terms:
Components are like functions or classes that return HTML elements to render on the screen.
2. React State – Formal Definition
State is a built-in JavaScript object used to store dynamic data within a component that determines how the component behaves and renders.
When the state of a component changes, React automatically re-renders that part of the UI to reflect the new data.
State is mutable, meaning it can be changed using thesetState()method in class components oruseState()Hook in functional components.
Example:
In short:
State represents data that can change over time and controls component re-rendering.
3. React Props – Formal Definition
Props (short for Properties) are read-only attributes used to pass data from one component (usually a parent) to another component (usually a child).
Props enable components to be dynamic and reusable, allowing information to flow unidirectionally (from parent to child).
Props are immutable, meaning they cannot be modified by the child component.
Example:
In short:
Props are input parameters that make components reusable and flexible by passing data between them.
Final Note (for Exams):
In React, Components are the building blocks of the UI, State represents data that can change, and Props are read-only values used for communication between components.
Together, they form the core architecture of React applications, enabling modular, reusable, and dynamic user interfaces.
React JS is an open-source JavaScript library developed by Facebook for building dynamic and interactive user interfaces (UI), especially for Single Page Applications (SPA).
It uses a component-based architecture, allowing developers to break down the UI into smaller, reusable parts called components.
React uses a Virtual DOM to enhance performance by updating only the changed portions of the page instead of reloading the entire DOM.
2. React Components
Definition:
A Component in React is a reusable piece of code that controls a part of the user interface.
Each component has its own structure (HTML), style (CSS), and logic (JavaScript). Components are like functions that return HTML elements.
Types of Components:
-
Functional Components
-
Class Components
(a) Functional Components
Functional components are simple JavaScript functions that return JSX (JavaScript XML).
They are preferred in modern React because of Hooks like useState and useEffect.
Example:
Explanation:
-
The
Welcomefunction is a component. -
It returns a JSX element (
<h1>tag). -
It can be reused anywhere in the application.
(b) Class Components
Class components are ES6 classes that extend from React.Component and must contain a render() method.
Example:
Explanation:
-
Class components have state and lifecycle methods.
-
The
render()method returns the JSX to display.
Features of React Components:
-
Reusability: Components can be used multiple times across an app.
-
Readability: Helps in organizing UI into smaller, understandable parts.
-
Maintainability: Easier to update and debug.
-
Modularity: Each component handles its own logic and appearance.
1.4 Component Structure
-
A React component typically contains:
-
Import statements (React, CSS, or other components)
-
Function/Class definition
-
JSX return statement
-
Export statement
-
Example Structure:
1.5 Creating and Exporting Components
-
Components can be created as functions or classes.
-
Use the
exportkeyword to make them available for use in other files.
Example:
1.6 Importing Components
-
Components are imported into other files using the
importstatement.
Example:
1.7 Nested Components
-
When a component is used inside another component, it’s called nesting.
-
This helps build a component hierarchy.
Example:
1.8 Rendering Components
-
Components are rendered inside the root element using
ReactDOM.createRoot()andrender().
Example:
3. React State
Definition:
State is a built-in object used to store dynamic data about the component.
It allows components to create and manage their own data which can change over time.
When the state changes, the component re-renders automatically, showing updated data in the UI.
Using State in Class Components:
Explanation:
-
The state object holds the variable
count. -
setState()updates the value. -
Each update re-renders the component automatically.
Using State in Functional Components (using Hooks):
Explanation:
-
useState(0)initializes state variablecountwith 0. -
setCountfunction updates the count. -
The component re-renders when the state changes.
Key Points about State:
-
Mutable: State can be changed using
setState()or Hooks. -
Local: Each component can have its own state.
-
Causes Re-render: Changing state triggers a re-render automatically.
-
Private: State is local to the component and cannot be accessed by others directly.
2.6 Event Handling and State Change
-
React handles events using camelCase syntax (e.g.,
onClick,onChange). -
Event functions update the state dynamically.
Example:
2.7 Multiple State Variables
-
Multiple states can be used within the same component using
useState.
Example:
2.8 State Lifecycle
-
React component states go through three main phases:
-
Initialization: Define the initial state.
-
Updation: State is modified using
setState()oruseState. -
Unmounting: Component is removed from the DOM.
-
2.9 Rules for Using State
-
Only call Hooks (like
useState) at the top level of the component. -
Only call Hooks inside React functions.
-
Do not modify state directly (always use
setStateorsetFunction). -
Keep state as simple and minimal as possible.
4. React Props (Properties)
Definition:
Props are read-only attributes used to pass data from parent component to child component.
They make components dynamic and reusable by sending values as parameters.
Example of Props:
Explanation:
-
Appis the parent component, andStudentis the child component. -
Data is passed through props like
nameandroll. -
Props make the
Studentcomponent reusable for multiple students.
Key Characteristics of Props:
-
Immutable: Props cannot be modified inside the child component.
-
Used for Communication: Props allow data flow from parent to child.
-
Reusable Components: Different values can be passed to create multiple instances.
3.5 Default Props
-
Default values can be defined for props using
defaultPropsproperty.
Example:
3.6 PropTypes Validation
-
Used to validate data types of props using the PropTypes library.
Example:
3.8 Nested Props
-
Passing objects or multiple data types as props (nested structure).
Example:
5. Difference Between State and Props
| Basis | State | Props |
|---|---|---|
| Definition | Manages internal data of the component | Used to pass data from parent to child |
| Mutability | Mutable (can be changed) | Immutable (cannot be changed) |
| Ownership | Owned by the component | Owned by the parent component |
| Updation | Can be updated using setState() or useState() | Cannot be updated directly |
| Usage | Used for dynamic data | Used for passing fixed or variable data |
| Example | Counter value changing | Passing name or ID to a component |
6. Example Combining Components, State, and Props
Explanation:
-
The App component manages the
messageusing state. -
The Message component receives the message via props.
-
When the button is clicked, state changes → message updates dynamically → re-render occurs.
7. Advantages of Using Components, States, and Props
-
Code Reusability: Components can be reused multiple times in different parts of the app.
-
Maintainability: Easier to debug since each component handles its own logic.
-
Dynamic UI: State allows components to update dynamically.
-
Unidirectional Data Flow: Props ensure data consistency by flowing in one direction.
-
Improved Performance: React updates only changed elements using Virtual DOM.
4.2 Parent-Child Communication
-
Communication is done via props — data flows from parent → child.
Example:
4.3 Updating State and Passing via Props
-
The parent updates state and sends the new value to the child via props.
Example:
4.4 Dynamic Rendering with State and Props
-
UI updates automatically when state changes, re-rendering components dynamically.
Example:
8. Real-World Example
Blog Application Example:
Output:
Initially:
After clicking button:
Content: React is fun to learn!