25
loading...
This website collects cookies to deliver better user experience
function sum(a, b) {
return a + b;
}
function multiplication(a, b) {
return a * b;
}
// result() is a Higher Order Function
function result(operation) { // operation is actually an another function
return function(a, b) { // return function
return "Result of operation: " + operation(a, b);
}
}
const sumResult = result(sum);
const multipleicationResult = result(multiplication);
console.log(sumResult(1, 200)); // Result of operation: 201
console.log(multipleicationResult(2, 200)); // Result of operation: 400
result()
is a higher order function which accepts the arithmetic operation as it's parameter.Result of operation:
sum()
, multiplication()
functions.HomePage
componentimport React from "react";
const HomePage = () => {
return (
<div>
<header>
Some Header Content
</header>
<div title="Home Page">
<h2>Home Page</h2>
</div>
<footer>
Some Footer Content
</footer>
</div>
);
};
export default HomePage;
AboutPage
componentimport React from "react";
const AboutPage = () => {
return (
<div>
<header>
Some Header Content
</header>
<div title="About Page">
<h2>About Page</h2>
</div>
<footer>
Some Footer Content
</footer>
</div>
);
};
export default AboutPage;
hocs/Layout.js
import React from "react";
const withLayout = (PageComponent) => {
return function WithPage({ ...props }) {
return (
<div>
<header>
Some Header Content
</header>
<!-- Called The Component Parameter -->
<PageComponent />
<footer>
Some Footer Content
</footer>
</div>
);
};
};
export default withLayout;
import React from "react";
import withLayout from "./hocs/Layout";
const HomePage = () => {
return (
<div title="Home Page">
<h2>HomePage</h2>
</div>
);
};
export default withLayout(HomePage);
import React from "react";
import withLayout from "./hocs/Layout";
const AboutPage = () => {
return (
<div title="About Page">
<h2>About Page</h2>
</div>
);
};
export default withLayout(AboutPage);
withLayout
hoc in any pages where we want.