Comments (7)
I found the solution:
In order to make it work, I had to specify a BaseComponent
as last argument of the branch
function. Nevertheless, I still don't get why the exemple from the doc works. (I assume that it works).
Does anyone have an explanation ? :)
from recompose.
Because branch
returns a function which accepts four arguments. Without providing BaseComponent
, what you get is just a function not a React component.
You can use lodash/identity
, which is a function arg => arg
, to achieve what you expect in a more semantic way.
export default branch(
props => !!props.title,
identity,
renderComponent(Loader)
)(Page)
from recompose.
How does that work in this exemple then ? The branch
function is not called with 4 arguments.
from recompose.
Just jumping in here: in the end branch
is called with the four arguments -- spinnerWhileLoading
seems to be a curried branch
function. (Please correct if I misunderstood something!)
from recompose.
@mrblueblue you are correct.
In the example:
const Post = spinnerWhileLoading(
// Here is the fourth argument
props => props.title && props.author && props.content,
// Here is a functional component
({ title, author, content }) => (
<article>
<h1>{title}</h1>
<h2>By {author.name}</h2>
<div>{content}</div>
</article>
)
);
from recompose.
Yeah I'm having issues getting this to work in compose
flow as well:
// This works, but I have to repeat the component to render after loading:
const preload = (test, Component) => branch(
test,
renderComponent(Loader),
renderComponent(Component)
);
export default compose(
provideHooks(mapHooks),
reduxForm(mapFormToProps, mapStateToProps, mapDispatchToProps),
preload(
(props) => props.isLoading,
MyComponent
)
)(MyComponent);
// This doesn't work, moved the Component 1 level down so we get it from `compose`.
// The weird thing is, I do see the Component class (MyComponent in this case)
// coming down correctly if I log out the args for the returned function...
const preload = (test) => (Component) => branch(
test,
renderComponent(Loader),
renderComponent(Component)
);
export default compose(
provideHooks(mapHooks),
reduxForm(mapFormToProps, mapStateToProps, mapDispatchToProps),
preload(
(props) => props.isLoading,
)
)(MyComponent);
/**
Results in this error:
Invariant Violation: Component(...): A valid React element (or null) must be returned.
You may have returned undefined, an array or some other invalid object.
*/
What's going on here? Am I missing something?
Update: so I got it working, but this code is very counterintuitive for me. Is this the way to do this?
const preload = (test) => (Component) => branch(
test,
renderComponent(Loader),
renderComponent(Component)
)(Component);
from recompose.
@StevenLangbroek You don't need the inner function. This will do:
const identity = t => t
const preload = (test) => branch(
test,
renderComponent(Loader),
identity
);
Sounds like the confusion arose from two things:
- It may be unclear to some that the 2nd and 3rd arguments to
branch
are higher-order components, not components. - Currying is confusing. We removed it in 0.17.0 for this reason.
Closing this issue, but if you have any other questions, let us know :)
from recompose.
Related Issues (20)
- renderNothing should export Nothing HOT 4
- Does TypeScript 3.4 enable typing of compose HOT 1
- Remove flow types in repo and support only in flow-typed repo HOT 1
- Key advantages in Docs HOT 2
- The best way to test a component that use getContext recompose HOT 1
- Get a lifecycle componentDidMount unmounted error HOT 1
- Rewriting mapPropsStream with Hooks or new Lifecycle Methods HOT 4
- compose hoc causes to lose typings
- Remove usage of `createFactory` HOT 4
- Why does `mapProps` remove `children`? HOT 2
- node-fetch vulnerability issue (denial of service) HOT 8
- Forked, Updated, and Published HOT 1
- fbjs dependency still present in lock file HOT 3
- Support React 17 HOT 2
- Suggest alternative to branch(), renderComponent(), renderNothing(), compose(), etc
- TypeError: Cannot read properties of undefined (reading 'apply')
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
- New patch required to mitigate high risk vulnerability
- Maintainers HOT 1
- This is GITHUB and not the European Parliament or NATO.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recompose.