Comments (16)
Base on the info in this thread, I propose we can add a new HOC, cond()
, to Recompose, which signature is similar to ramda/cond
:
[[(props → Boolean), HOC]] → HOC
Ex:
cond([
[props => props.error, renderComponent(ErrorComponent)],
[props => props.warning, renderComponent(WarningComponent)],
[() => true, a => a]
])
from recompose.
@acdlite I think your resolution was good but there is definitely a place for a switch()
that operates on HOC. Is there another issue that attends to this?
I would like to handle 4 cases for a prop:
missing | instanceof Error | empty List | default (non-empty list)
Currently I need to compose 3 x branch
HOCs to do this.
from recompose.
And IMO we should let the consumer decide how to compose their HOC, instead of wrapping renderComponent
for them.
from recompose.
@timkindberg They're similar. However, cond
is familiar to ramda
users :)
from recompose.
@barbalex No, it hasn't been added.
from recompose.
I also think it would be very useful and I'm happy to implement it if needed.
from recompose.
Thanks for your suggestions! I'm hesitant to add a conditional()
helper because it's not clear from the name how it's different from branch()
. However, I've been thinking about adding a renderComponent()
helper, which is really just the identity function with a more meaningful name This isn't quite true, clarification below. With this helper, you could achieve the same effect as conditional()
like so:
const Component = branch(
test,
renderComponent(Foo), // Foo is rendered if true
renderComponent(Bar) // Bar is rendered if false
)(BaseComponent)
This involves a bit more typing, but it avoids the confusion of whether branch()
accepts an HoC or a component. I'm open to also adding your proposed conditional()
helper if you can think of a better name for it.
Your switch()
idea is also neat, but again, I would want it to operate on HoCs, not components.
EDIT:
renderComponent()
would look like this:
const renderComponent = Component => () => Component;
For stateless function components—but not class components—you could replace this with the identity function.
from recompose.
I've added renderComponent()
to version 0.5.0. There's an example of how to use it here: https://github.com/acdlite/recompose/blob/master/docs/API.md#rendercomponent
from recompose.
Having the same "issue" as @bholloway. I need to render an Avatar
based on either:
userId
: fetch user from api
user
: should contain all info
picture
: render simple image
anonymous
: show guest icon
<Avatar userId="u1" />
<Avatar user={{ picture: '...' }} />
<Avatar picture="http://....." />
<Avatar />
from recompose.
The cond
would be awesome.
from recompose.
I wonder how similar this is to a helper I wrote called nonOptimalStates
?
from recompose.
has cond
(#21 (comment)) been added?
It is not documented: https://github.com/acdlite/recompose/blob/master/docs/API.md
from recompose.
Even though it has been awhile and in most cases it is recommended to use switch (#386 (comment)), in case you actually want to use a switch as an HOC -
(The definition is based on #21 (comment) and the implementation is mostly inspired by branch implementation)
import { Component, ComponentType, createFactory } from 'react';
import { ComponentEnhancer, InferableComponentEnhancer, setDisplayName, wrapDisplayName } from 'recompose';
type mapper<TInner, TOuter> = (input: TInner) => TOuter;
type predicate<T> = mapper<T, boolean>;
type Condition<TOuter> = [predicate<TOuter>, ComponentEnhancer<any, any> | InferableComponentEnhancer<{}>];
const identity = Component => Component;
export function cond<TOuter = any>(conditions: Array<Condition<TOuter>>): ComponentEnhancer<any, TOuter> {
return (BaseComponent: ComponentType<any>) => {
const factories = Array(conditions.length);
class Cond extends Component<TOuter> {
render() {
const enhancerIndex = conditions.findIndex(([test, hoc]) => test(this.props));
const enhancer = enhancerIndex > -1 ? conditions[enhancerIndex][1] : identity;
factories[enhancerIndex] = factories[enhancerIndex] || createFactory(enhancer(BaseComponent));
return factories[enhancerIndex](this.props);
}
}
if (process.env.NODE_ENV !== 'production') {
return setDisplayName<TOuter>(wrapDisplayName(BaseComponent, 'cond'))(Cond);
}
return Cond;
};
}
from recompose.
@wuct @arturmuller this issue was closed, maybe reopen it, or open a new one with the cond
proposal ?
from recompose.
Here's how I've ended up using branch()
wrapped around a "null" component:
export default branch(
props => !!canRenderBaseComponent(props),
renderComponent(BaseComponent),
renderNothing
)(() => null);
from recompose.
Had to reinvent this just now, called it branches
to avoid import name clash with cond
from ramda.
import {
reduceRight,
} from 'ramda';
import {
branch,
renderNothing,
} from 'recompose';
export const branches = reduceRight(([ test, left ], right) => branch(test, left, right), renderNothing);
Usage example:
import {
prop,
T as alwaysTrue,
} from 'ramda';
import {
compose,
renderNothing,
renderComponent,
} from 'recompose';
import { branches } from '../../utils/branches';
const UserDetails = () => 'UserDetails';
const TransactionDetails = () => 'TransactionDetails';
const OverallDetails = () => 'OverallDetails';
export const Details = compose(
/* ... */
branches([
[ prop('selectedUserId'), renderComponent(UserDetails) ],
[ prop('selectedTransactionId'), renderComponent(TransactionDetails) ],
[ alwaysTrue, renderComponent(OverallDetails) ],
])
)(renderNothing);
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.