0

am trying to dynamically import an icon from react-icons-kit and its throwing this error :

 Module not found: Can't resolve 'enzyme' in 'G:\my-app\node_modules\react-icons-kit'

I have been trying for an hour and nothing.


My code :

import React from 'react';
import { Icon } from 'react-icons-kit';

const Button = ({ icon = 'home', library = "fa", children, ...props }) => {

    if (icon) {
        var svg = require('react-icons-kit/' + library + '/' + icon);
    }

    return (
        <button className={classes} {...props}>
            {children}
            <Icon icon={svg}/>
        </button>
    );
};

export default Button;

what am I missing?

1 Answer 1

1

You can't dynamically import that way, as your bundler needs to know all dependencies when the application is built. If you absolutely need to use this dynamic route, you can use require.context to load the entire folder, and then dynamically load them from that:

import React from 'react';
import { Icon } from 'react-icons-kit';
const iconRequire = require.context('react-icons-kit/', true);

const Button = ({ icon = 'home', library = "fa", children, ...props }) => {

    if (icon) {
        var svg = iconRequire('./' + library + '/' + icon);
    }

    return (
        <button className={classes} {...props}>
            {children}
            <Icon icon={svg}/>
        </button>
    );
};

export default Button;

However, it would be much more efficient to pass the actual icon to your Button class:

import homeIcon from 'react-icon-kit/fa/home';

// Call it below, and drop the strings all together:

<Button icon={homeIcon} />

This is more efficient and will reduce your bundle size down tremendously (As you don't need to include a whole bunch of extra icons you may not even use).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.