Skip to content

Instantly share code, notes, and snippets.

@adi518
Last active October 31, 2019 19:05
Show Gist options
  • Save adi518/7861543f16c9a65a289687288e67e807 to your computer and use it in GitHub Desktop.
Save adi518/7861543f16c9a65a289687288e67e807 to your computer and use it in GitHub Desktop.
Higher-order React component for providing fallback content while server-dependent props are absent.
// `MyComponent` depends on server-data, so it will render nothing
// or a fallback component until fetching is completed.
const withFallback = composeFallback(({ serverData }) => [serverData]);
export default withFallback(MyComponent);
// https://reactjs.org/docs/concurrent-mode-suspense.html
import React from "react";
import { isNil } from "lodash/fp";
export const composeFallback = (mapPropsToAsyncProps = () => []) => (
WrappedComponent,
Fallback = null
) => props =>
[].concat(mapPropsToAsyncProps(props)).some(asyncProp => isNil(asyncProp)) ? (
Fallback
) : (
<WrappedComponent {...props} />
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment