Skip to main content

Command Palette

Search for a command to run...

What is connect()() function in redux or reactjs ?

Currying vs partial function in javascript

Updated
2 min read
M

Currently, I am publishing the blog for Jan month updates in our technology fields, what is OOPS under the hoods in JavaScript?, Understand the flexbox CSS Under 5 min.

In redux, we usually comes across connect()() syntax.

Everyone knows that, connect()() function in redux is used for connecting the component with store.

But under the hood, what it exactly means? What can we call such functions ? Is this are normal function like foo() thing ?

Lets see whats its exactly:

What is knowns as ? Currying: This methodology or syntax signature of function which takes multiple arguments one at a time is known as 'Curried function' or in short 'Currying'

Is it same as normal / partial function foo()? Curry: lets you call a function, splitting it in multiple calls, providing one argument per-call.

Partial: lets you call a function, splitting it in multiple calls, providing multiple arguments per-call.

Basically both are same, Currying function helps you to manage code better that partial function and that's the reason on architecture level, usually you will come across curry functions.

Example: Lets do a sum using both partial and curry function:

Partial function:

function sum_partial(a,b,c){
    return a+b+c;
}

Curried Function:

function sum_curried(a) {
    return function (b) {
        return function (c)  {
            return a + b + c
        }
    }
}

Calling partial function:

let res = sum_partial(1, 2, 3);
console.log(res); //6

Calling Curried function: //Method ONE

let sc1 = sum_curried(1);
let sc2 = sc1(2);
let res2 = sc2(3);
console.log(res2); //6

Short METHOD OR Similar to connect()() in redux

let res3 = sum_curried(1)(2)(3);
console.log(res3); //6

Working JS Fiddle here

For more such contents follow Mohammed Sabir

Cheers!!

M

Whats your thoughts?

More from this blog

IMBlog

3 posts

I am a web developer with expertise in cutting edge technologies, technical writer, technical recruiter. I believe in #Gratitude. ✨✨✨. My techstak include HTML,CSS,Bootstrap and JS, PHP, Laravel, Node