React Material UI圖示的動態導入遇到問題
P粉627136450
P粉627136450 2023-08-31 09:14:27
0
1
489
<p>你好,我正在嘗試從mui動態加載圖標,以下是代碼:</p> <pre class="brush:php;toolbar:false;">import React from "react"; import * as MuiIcons from "@mui/icons-material"; console.log("MuiIcons: ", MuiIcons); const Icon = ({ iconName }) => { return <div>{MuiIcons[iconName]}</div>; }; export default Icon;</pre> <p>但我得到了以下錯誤:</p> <pre class="brush:php;toolbar:false;">react-dom.development.js:14887 Uncaught Error: Objects are not valid as a React child (found: object with keys {$$type, type, compare}). If you meant to render a collection of children, use an array instead. at throwOnInvalidObjectType (react-dom.development.js:14887:9) at reconcileChildFibers2 (react-dom.development.js:15828:7) at reconcileChildren (react-dom.development.js:19167:28) at updateHostComponent (react-dom.development.js:19924:3) at beginWork (react-dom.development.js:21618:14) at HTMLUnknownElement.callCallback2 (react-dom.development.js:4164:14) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:16) at invokeGuardedCallback (react-dom.development.js:4277:31) at beginWork$1 (react-dom.development.js:27451:7) at performUnitOfWork (react-dom.development.js:26557:12)</pre> <p>有解決方法嗎?提前謝謝你。 </p>
P粉627136450
P粉627136450

全部回覆(1)
P粉198670603

您正在匯入一個物件而不是一個React元件。

要以這種方式使用它,您可以這樣做:

const Icon = ({ iconName }) => {
  const Icon = MuiIcons[iconName]
  return <Icon />
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!