Last active
October 31, 2019 19:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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