0

The title is not good and I'll edit it as soon as I find out a name for this.

I've been learning Redux and it's the first place I've seen this kind of function.

export default connect(null, actionCreators)(TodoApp)

I can't get my head around, clearly "null" and "actionCreators" are params passed to the connect function, but what is "(TodoApp)" doing?

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
Borjante
  • 9,767
  • 6
  • 35
  • 61

2 Answers2

2

Javascript supports first class functions this is, they can be passed around like normal data. So connect(null, actionCreators) returns an anonymous function to which TodoApp is passed to.

A simple understandable example would be:

var obj = {
  name: "Test",
  age: 10
}

var f = () => {
  // Do whatever f function should do
  
  // Return an anonymous function that takes in an object as parameter
  return (obj) => {
    
    // Do whatever we want to to with the object
    console.log(obj.name)
  }
}

// These two methods are equivalent
  // 1.- 
  f()(obj);

  // 2.-
  var f2 = f();
  f2(obj);
Borjante
  • 9,767
  • 6
  • 35
  • 61
1
connect(null, actionCreators)(TodoApp);

is as good as :

var fun = connect(null, actionCreators);
fun(TodoApp);

Its something like this :

function connect(val1,val2){
  console.log(val1,val2);
  return function(val3){
    console.log(val3);  
  }
}

var actionCreators = "X";
var TodoApp = "Some Object";

connect(null, actionCreators)(TodoApp);

As you can see, connect takes two inputs,

Returns an anonymous method, which takes one input.

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43