32
loading...
This website collects cookies to deliver better user experience
map()
array method as an example. This method takes a function as a parameter which means that it is a higher-order function, but how are we recycling logic with this method? well, the map()
method does the following:const arr = [1, 2, 3]
const mappedArr = arr.map(number => number * 2)
console.log(mappedArr)
//[2, 4, 6]
map()
method.const add = (num1) => (num2) => num1 + num2
const addFive = add(5)
// addFive = (num2) => 5 + num2
console.log(addFive(12)) // 5 + 12 = 17
// 17
(num2) => 5 + num2
so we basically used our higher-order function to build another function that adds 5 to any number.const withComponent = Component => () => <Component />
const someComponent = () => (
<div>
Hi
</div>
)
const sameComponent = withComponent(someComponent)