2

What does the : stateProps part of this mean? I know that the whole mapStateToProps function is optional and that it takes the first argument of state and an optional second argument of ownProps, but what does a : designate? Is this what the function will return?

[mapStateToProps(state, [ownProps]): stateProps]
Mogsdad
  • 40,814
  • 19
  • 140
  • 246
stackjlei
  • 7,747
  • 12
  • 42
  • 95

1 Answers1

2

My understanding is that you are correct: The information after the colon, in this case, represents the type of the value returned from the function. In other words, this function will return values that can be accessed as properties of the connected component and, more specifically, will be props that hold some representation of state, and thus the name stateProps.

I don't know where there is an official (or even unofficial) description of this type of syntax for function signatures for JavaScript in general or react/redux specifically. However, I did find this other StackOverflow answer that discusses this kind of syntax for TypeScript code. I've also seen this type of function signature syntax for, e.g., PHP (e.g. this other StackOverflow question).

Note that a colon followed by a type can refer not only to the type of the value returned from a function but also, if found within the parentheses of the function signature, to the type of a function parameter. For example, in this GitHub description of Flux, you find the following line: reduce(state: number, action: Object): number {.... This means that this particular function, named reduce, should be called with two parameters, a parameter named state of type "number" and a parameter named action of type "Object", and it will return a value of type "number".

Community
  • 1
  • 1
Andrew Willems
  • 9,768
  • 8
  • 39
  • 58