-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHOC.jsx
More file actions
36 lines (30 loc) · 948 Bytes
/
HOC.jsx
File metadata and controls
36 lines (30 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// HOC.js
import React from 'react';
/*
* ✅ HOC Definition:
* A Higher-Order Component is a function that takes a component and returns a new component.
* It is used for code reuse and adding behavior to existing components.
*/
const withLogger = (WrappedComponent) => {
return (props) => {
console.log(`Rendering ${WrappedComponent.name} with props:`, props);
const upperName = props.name.toUpperCase();
return <WrappedComponent {...props} name={upperName} />;
};
};
// 🔹 Normal Component
const UserGreeting = ({ name }) => {
return <h2>Hello, {name}!</h2>;
};
// 🔹 Enhanced Component using HOC
const LoggedUserGreeting = withLogger(UserGreeting);
const HOC = () => {
return (
<div style={{ padding: 20 }}>
<h1>React Higher-Order Component (HOC) Example</h1>
{/* This logs props to console before rendering */}
<LoggedUserGreeting name="Jeevan" />
</div>
);
};
export default HOC;