<ConditionalSpinner renderIf={data}><PersonRenderer person={data.person} /></ConditionalSpinner>
Could be written better as: <PersonRenderer person={data.person} />
Where PersonRenderer would be defined as such: class PersonRenderer extends Component {
render() {
if(this.props.person) {
return <div>personcontent</div>
}
return <LoadingIndicator/>
}
}
Given this, I'm still not sure how an If "component" would be achieved (and further, you'd need an Else or a Switch component anyway if going down this route). Maybe you could explain the benefits of such a path? function withLoadingIndicator(Wrapped) {
return function(props) {
if (props.loading) {
return (<LoadingIndicator />);
}
return (<Wrapped {...props} />);
}
}
// ...
export class PersonRenderer extends Component {
render() {
return <div>personcontent</div>
}
}
export default withLoadingIndicator(PersonRenderer);It's often divided in two: One container fetching the data, and a pure component rendering it.
The "states" aren't related to container/dumb components or data-fetching/selection logic. They're purely about rendering the state for a given segment of the page. Perhaps this article[0] might help convey my intent. It's more about how to render "we didn't get data from that one endpoint for this section of the page" or "we're waiting for the data to show up", etc.
[0]: https://medium.com/swlh/the-nine-states-of-design-5bfe9b3d6d...
I usually use tests at the top of the render function to make sure that data is there, and return cleanly if it isn't. It just makes development easier as React components tend to get re-used. Within the data.person, in your example, the caller should make sure that it is complete, or pass nothing at all.
In the case you show I would either return <Spinner/> or the content, but never nest them. Have a conditional and two return statements, or one return statement with a ternary operator.
People might argue that this isn't the React way of doing things, but it's easier than setting a conditional classname to display none. It's also easier than breaking out a small amount of html into a separate component just because you want some conditional display logic.
function ConditionalSpinner({renderIf, children}) {
if (!renderIf) return null;
return children[0];
} <div *if={cond1 && cond2}>
<div>foo</div>
</div>
// results to
if (cond1 && cond2) {
return React.createElement('div', null,
React.createElement('div', null, 'foo')
);
}
I'm not sure why this is bad? Because it's looking like Angular?!! {cond1 && cond2
? <div>foo</div>
: null
} {(() => {
if (cond1 && cond2)
return <div>foo</div>
else
...
})()}
Though it makes GitHub think it's a Clojure file. {(() => {
})()}
Honestly I can't decide if I think this is ugly or beautiful. It's like the buffalo buffalo of JS. {do {
if (cond1 && cond2)
<div>foo</div>
else
...
}}
If JSX decides to implement "implicit" "do expressions", I suppose it would just look like this: {
if (cond1 && cond2)
<div>foo</div>
else
...
}
http://wiki.ecmascript.org/doku.php?id=strawman:do_expressio...note: i've changed your example to show code inside html tags, i'm aware it does something different to yours.
conditionals
<div>
@if(cond1 && cond2) {
<div>foo</div>
}
</div>
loops <div>
@for(var i in collection) {
<div></div>
}
</div>
variables <div>
@variable
</div>
ternary/longer statement <div>
@(variable ? 1 : 2)
</div>Maybe just a personal thing.
translates to
React.createElement('div', {if: <expression>})
This is bad because createElement always gets execute, whether the if expression evaluated to true or false. Changing this semantic would be messy.
<ConditionalSpinner renderIf={data}><PersonRenderer person={data.person} /></ConditionalSpinner>
Problem is that the inner component will still be parsed and fail with cannot read property person of undefined. Yes, you can do this with a simple condition in your render function, but then it's not declarative any more.