如何动态导入一个React组件并调用它?

huangapple go评论81阅读模式
英文:

how to dynamically import a react component and call it?

问题

I have a react component that contains an SVG image.
and i import it like this

import MessageIcon from '../static/styles/svg/messageIcon';

However as part of code splitting, i want to dynamically import it instead of static import.

I tried like this but does not work

import('../static/styles/svg/messageIcon').then(MessageIcon => { return <MessageIcon /> });

can anyone tell me how i can dynamically import and call this component? thanks in advance

英文:

I have a react component that contains an SVG image.
and i import it like this

import MessageIcon from &#39;../static/styles/svg/messageIcon&#39;;

However as part of code splitting, i want to dynamically import it instead of static import.

I tried like this but does not work

import(&#39;../static/styles/svg/messageIcon&#39;).then(MessageIcon =&gt; {
return &lt;MessageIcon /&gt;
});

can anyone tell me how i can dynamically import and call this component? thanks in advance

答案1

得分: 1

以下是您要翻译的内容:

您可以像这样导入动态内容:

import React, { useEffect, useState } from "react";

const App = () => {
    const [state, setState] = useState<any>();

    useEffect(() => {
        (async () => {
            const i = await import("../../assets/img/user.svg");
            setState(i);
        })();
    }, []);
    
    return <img alt={"test dynamic"} src={state?.default} />;
};
export default App;

您还可以使用 ref 来编写它:

import React, { useEffect, useRef } from "react";

const App = () => {
    const ref = useRef<any>();

    useEffect(() => {
        (async () => {
            const i = await import("../../assets/img/user.svg");
            ref.current.src = i.default;
        })();
    }, []);
    
    return <img alt={"test dynamic"} ref={ref} />;
};
export default App;

导入动态的 React 组件:

import React from "react";

const App = () => {
    const nameIcon = "iconMessage";
    const IconMessage = React.lazy(() => import(/* webpackChunkName: "icon" */ `./${nameIcon}`));

    return <IconMessage />;
};
export default App;

请注意,我已经将代码部分保留在原始语言中,没有进行翻译。如果您需要更多帮助,请告诉我。

英文:

You can import dynamical like this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, { useEffect, useState } from &quot;react&quot;;

const App = () =&gt; {
    const [state, setState] = useState&lt;any&gt;();

    useEffect(() =&gt; {
        (async () =&gt; {
            const i = await import(&quot;../../assets/img/user.svg&quot;);
            setState(i);
        })();
    }, []);
    
    return &lt;img alt={&quot;test dynamic&quot;} src={state?.default} /&gt;;
};
export default App;

<!-- end snippet -->

Also, you can write it with ref too:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    import React, { useEffect, useRef } from &quot;react&quot;;

    const App = () =&gt; {
        const ref = useRef&lt;any&gt;();

        useEffect(() =&gt; {
            (async () =&gt; {
                const i = await import(&quot;../../assets/img/user.svg&quot;);
                ref.current.src = i.default;
            })();
        }, []);
        
        return &lt;img alt={&quot;test dynamic&quot;} ref={ref} /&gt;;
    };
    export default App;

<!-- end snippet -->

import dynamical React component

import React from &quot;react&quot;;

const App = () =&gt; {
      const nameIcon = &quot;iconMessage&quot;;
      const IconMessage = React.lazy(() =&gt; import(/* webpackChunkName: &quot;icon&quot; */ `./${nameIcon}`));

      return &lt;IconMessage /&gt;;
};
export default App;

huangapple
  • 本文由 发表于 2023年1月6日 13:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定