question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connect currying does not work properly [cerebral@2]

See original GitHub issue

The code I’m interesting in

src/viewFactories/connect.js:

export default HOC => function connect (paths, passedSignals, injectedProps, passedComponent) {
  let component = passedComponent
  let signals = passedSignals
  let props = injectedProps

  if (arguments.length === 3) {
    component = props
    props = null
  } else if (arguments.length === 2) {
    component = signals
    signals = null
  }

  if (!component) {
    return function (decoratedComponent) {
      return HOC(paths, signals, props, decoratedComponent)
    }
  }

  return HOC(paths, signals, props, component)
}

Problem

The connect function contains this bit of code:

  if (!component) {
    return function (decoratedComponent) {
      return HOC(paths, signals, props, decoratedComponent)
    }
  }

which seems to allow currying and so using connect as a Decorator (or in compose for Stateless component).

But this code does not work because before it, arguments are re-assigned depending on arguments.length :

  if (arguments.length === 3) {
    component = props
    props = null
  } else if (arguments.length === 2) {
    component = signals
    signals = null
  }

Because of that the following code does not works

connect(paths, signals)(Component);
// or
@connect(paths, signals)
class Component {}
// or
compose(
  connect(paths, signals)
)(Component);

In this case the arguments.length === 2 condition is true and signals are sets in component… and HOC is called with the following argument HOC(paths,null, null, signals) which is 👎.

This mean the only way to make currying works is the following :

@connect(paths, signals, props, null)(Component);

Which is quite annoying…

Possible solution

I think a better API would be the same as redux : alway returning a HOC function instead of passing the component as the last argument.

Note

I’m using cerebral@next which resolve to cerebral@2.0.0-0-alpha.5a3d129e.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
christianalfonicommented, Nov 20, 2016
1reaction
etienne-dldccommented, Nov 20, 2016

@christianalfoni I think @Guria 's solution might be better because it’s more simple and does not need to import all possible types of component (React and Inferno for now).

@Guria Yes, I will make a PR soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript function currying does not work on instance method
I know this error is nothing to do with currying but I am messing up with some basics of JS functions concept. So...
Read more >
Everything about Currying in JavaScript | by Chidume Nnamdi
Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting ...
Read more >
Javascript- Currying VS Partial Application | by Deepak Gupta
Currying is not partial application. It can be implemented using partial application. You can't curry a function that takes any number of arguments,...
Read more >
Functional Programming: Currying in TypeScript
Definition: Currying is transforming a function by a fixed arity(number of given arguments) to a function that is a sequence of nested returned...
Read more >
A Beginner's Guide to Currying in Functional JavaScript
But currying is actually a very simple concept, and it addresses some familiar problems when dealing with function arguments, while opening ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found