Skip to content

Instantly share code, notes, and snippets.

View frivolta's full-sized avatar

Filippo Rivolta frivolta

View GitHub Profile
@frivolta
frivolta / react-hoc4.jsx
Created May 27, 2022 15:46
Write better React, compose multiple functional HoCs, Higher-Order Components - compose hoc's
const App = (props) => {
return (
<div className="App">
<h1>A component: {props.title}</h1>
<h2>User: {props.customUser.email}</h2>
</div>
);
};
export default compose(withUser, withTitle)(App);
@frivolta
frivolta / react-hoc3.jsx
Created May 27, 2022 15:41
Write better React, compose multiple functional HoCs, Higher-Order Components - HoC
export const withTitle = (Component) => (props) => {
const title = "Custom title";
return <Component title={title} {...props} />;
};
@frivolta
frivolta / react-hoc2.jsx
Created May 27, 2022 15:38
Write better React, compose multiple functional HoCs, Higher-Order Components - Wrapped Component
// A page component that just render text
const App = (props) => {
return (
<div className="App">
<h1>A component</h1>
<h2>User: {props.currentUser.email}</h2>
</div>
);
};
// Wrapping with withUser function
@frivolta
frivolta / react-hoc1.jsx
Created May 27, 2022 15:37
Write better React, compose multiple functional HoCs, Higher-Order Components - Higher Order Component
export const withUser = (Component) => (props) => {
// Passing the user that you fetched
const currentUser = { authtenticated: true, email: "email@email.com" };
return <Component currentUser={currentUser} {...props} />;
};
@frivolta
frivolta / react-component.jsx
Created May 27, 2022 15:35
Write better React, compose multiple functional HoCs, Higher-Order Components - Component
// A page component that just render text
const App = (props) => {
return (
<div className="App">
<h1>A component</h1>
</div>
);
};
export default App;
@frivolta
frivolta / custom-hof.js
Created May 27, 2022 15:31
Write better React, compose multiple functional HoCs, Higher-Order Components - Custom Higher Order Function
// We first define the function we will be using as an argument
const addOne = (arg)=>arg+1
// We than define our hof
const higherOrderFunction = (fn, arg) => fn(arg)*2
// The result will be 12
higherOrderFunction(addOne, 5)
@frivolta
frivolta / hof.js
Created May 27, 2022 15:28
Write better React, compose multiple functional HoCs, Higher-Order Components - Array.prototype.map
// Result is [2,3,4]
[1,2,3].map((number)=>number+1)
// Note that you can extract the callback function and pass it to the map function:
function addOne(arg){
return arg+1
}
[1,2,3].map((number)=>addOne(number))
// or
const multiply20 = (price) => price * 20;
const divide100 = (price) => price / 100;
const normalizePrice = (price) => price.toFixed(2);
const addPrefix = (price) => "$" + String(price);
const pipe =
(...fns) =>
(x) =>
fns.reduce((res, fn) => fn(res), x);
const compose =
const pipe =
(...fns) =>
(x) =>
fns.reduce((res, fn) => fn(res), x);
const addPrefix = (price) => "$" + String(price); //$ 40.00
const discountWithPrefix = compose(
addPrefix,
normalizePrice,
divide100,
multiply20
);
discountWithPrefix(200.0); // '$40.00'