SHARE
TWEET

Untitled

a guest Jun 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import loadable from '@loadable/component';
  2.  
  3. const validIconNames = ['Close', 'Stop', 'Play', 'Image'];
  4.  
  5. export const loadIcon = (iconName) => {
  6.   // validate that this icon will exist
  7.   if (!validIconNames.includes(iconName)) return null;
  8.   try {
  9.     return loadable.lib(() => import(/* webpackExclude: /\.spec\.js$/ */ `../${iconName}`));
  10.   } catch (error) {
  11.     console.warn(`Failed to load icon ${iconName}. ${error}`);
  12.     return null;
  13.   }
  14. };
  15.  
  16. const Component = ({iconName}) => {
  17.   const LoadableIcon = loadIcon(iconName) || null; // we validate inside the function so no need here
  18.   return (
  19.       {LoadableIcon && <LoadableIcon>{({ default: Icon }) => <Icon />}</LoadableIcon>}
  20.    );
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top