Skip to content

Instantly share code, notes, and snippets.

@PaquitoSoft
Created December 10, 2020 12:14
Show Gist options
  • Save PaquitoSoft/52223913ab51403a1912cbdee0e35a19 to your computer and use it in GitHub Desktop.
Save PaquitoSoft/52223913ab51403a1912cbdee0e35a19 to your computer and use it in GitHub Desktop.
import React, { Suspense } from 'react';
import PropTypes from 'prop-types';
import { useResponsive } from './responsive-provider';
const SuspenseFallback = () => (null);
function ResponsiveComponent({
xs,
sm,
lg,
md,
xl,
xxl,
loadingComponent,
children,
...props
}) {
const { selectResponsiveComponent } = useResponsive();
const Fallback = loadingComponent || SuspenseFallback;
const Component = selectResponsiveComponent({ xs, sm, md, lg, xl, xxl });
return (
<Suspense fallback={<Fallback />}>
<Component {...props}>{children}</Component>
</Suspense>
);
}
ResponsiveComponent.propTypes = {
xs: PropTypes.func,
sm: PropTypes.func,
md: PropTypes.func,
lg: PropTypes.func,
xl: PropTypes.func,
xxl: PropTypes.func,
loadingComponent: PropTypes.elementType,
children: PropTypes.node
};
export default ResponsiveComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment