In React, a component is a reusable building block
for creating user interfaces.
// 1. Import the React library
import React from 'react';
// 2. Define a functional component
function Component() {
// 3. Return JSX to describe the component's UI
return (
<div>
<h1>I am a React Reusable Component</h1>
</div>
);
}
// 4. Export the component to make it available for use in other files
export default Component;
Types of Components:
- Functional Components
- Class based Components
Functional Components
-
Functional components are declared as Javascript function
-
They are
stateless components
, but with the help of hooks, they can now manage state also.
Class based Components
-
Class-based components are declared as Javascript ES6 classes.
-
They are
stateful components
, by using life cycle methods. (meaning they can manage and maintain their own state using this.state.) -
The render method is a class component is responsible for returning JSX.
props (properties) are a way to pass data
from a parent component to a child component.
function App() {
return (
<ChildComponent name="Happy" purpose="Interview" />
);
}
function ChildComponent(props) {
return <div>{props.name}, {props.purpose}!</div>;
}
Prop drilling is the process of passing down props through multiple layers of components.
Parent Component -> Child Component -> Grand Child Component
Data is passed down through each component.
function PropParent() {
return (
<div>
<PropChild message={"data"} />
</div>
);
}
function PropChild({ message }) {
return (
<div>
<PropGrandChild message={message} />
</div>
);
}
function PropGrandChild({ message }) {
return (
<div>
<h3>{message}</h3>
</div>
);
}
- Maintenance: Prop drilling can make code harder to maintain as changes in data flow require updates across multiple components.
- Complexity: It increases code complexity and reduces code readability.
- Debugging: Debugging becomes challenging when props need to be traced through numerous components.
- Using Context API
- Using Redux
- Using Component Composition
- Using Callback Functions
- Using Custom Hooks
Class based Components
-
Class-based components are declared as Javascript ES6 classes.
-
They are
stateful components
, by using life cycle methods. (meaning they can manage and maintain their own state using this.state.) -
The render method is a class component is responsible for returning JSX.
import React, {Components} from 'react'; class AppClass extends Component { render() { return <h1>Hello World!</h1> } } export default AppClass;
this.props
can be used in child component to access properties/data passed from parent component.
class ParentComponent extends Component {
render() {
const dataToSend = "Hello from Parent!";
return (
<div>
<ChildComponent message={dataToSend} />
</div>
);
}
}
export default ParentComponent;
class ChildComponent extends Component {
render() {
return (
<div>
<p>Message: {this.props.message}</p>
</div>
);
}
}
export default ChildComponent;
this keyword is used to refer to the instance of the class
.
Functional Component | Class Component |
---|---|
Syntax: Defined as a JS function. | Syntax: Defined as a JS (ES6) class. |
State: Originally stateless but can now maintain state using hooks. | State: Can manage local state with this.state . |
Lifecycle methods: No | Lifecycle methods: Yes |
Readability: More readable & concise. 🏆 | Readability: Verbose (complex). |
this keyword: No | this keyword: Yes (Access props using this.props ) |
Render method: Do not have render method. | Render method: Have render method. |