Connect currying does not work properly [cerebral@2]
See original GitHub issueThe 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:
- Created 7 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
I guess this can be closed?
https://github.com/cerebral/cerebral/pull/470
@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.