So I actually ran into a use case where it is beneficial to render as a component rather than a function call. With React 16, you get the error boundaries feature. This allows you to render fallback error UIs when an error is thrown within the component. Turns out, if an exception is thrown in the function call variation, it won't trigger componentDidCatch. It needs to be thrown within a child component.
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {
error: false
};
}
componentDidCatch() {
this.setState({ error: true});
}
render() {
return this.state.error
? "Error :("
: this.props.children;
}
}
const renderContent = () => {
throw new Error();
}
const Content = () => {
throw new Error();
}
// This will throw exception and not trigger error state
const Foo = () => (
<ErrorBoundary>
<div>{renderContent()}</div>
</ErrorBoundary>
);
// This will trigger the error state
const Bar = () => (
<ErrorBoundary>
<div><Content /></div>
</ErrorBoundary>
);
Of course, you could have an error boundary higher up, but just pointing out one specific use case where you may choose one of the other.
Also, it's nice to render as component for naming purposes. It will show up named in React dev tools, and also you can use the name as selectors when you do Enzyme testing.