使用从另一个文件导入的 react-icons。

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

Using react-icons that are imported from another file

问题

// constants.js
import { MdHomeIcon, MdCodeIcon } from "react-icons/md";

export const categories = [
  { name: "New", icon: <MdHomeIcon /> },
  { name: "Coding", icon: <MdCodeIcon /> },
  { name: "ReactJS", icon: <MdCodeIcon /> },
];
// Sidebar.jsx
import { categories } from "./constants.js";

export const Sidebar = () => (
  <div>
    {categories.map((category) => (
      <div>
        <span>{category.icon}</span>
        <span>{category.name}</span>
      </div>
    ))}
  </div>
);
英文:

In a react app (created by vite), I need a list of categories to create a navigation menu. I have created this list in another file with .js extension. In the form of an array of objects:

// constants.js
import { MdHomeIcon, MdCodeIcon } from &quot;react-icons/md&quot;;

export const categories = [
  { name: &quot;New&quot;, icon: &lt;MdHomeIcon /&gt; },
  { name: &quot;Coding&quot;, icon: &lt;MdCodeIcon /&gt; },
  { name: &quot;ReactJS&quot;, icon: &lt;MdCodeIcon /&gt; },
];

react-icons package is installed.

My problem is this: before doing anything, react app return a syntax error in console: Uncaught SyntaxError: expected expression, got '&lt;' in constants.js.

after solving the problem: In Sidebar.jsx, I want to map through categories to build a list with names and icons.:

// Sidebar.jsx
import { categories } from &quot;./constants.js&quot;;

export const Sidebar = () =&gt; (
  &lt;div&gt;
    {categories.map((category) =&gt; (
      &lt;div&gt;
        &lt;span&gt;{category.icon}&lt;/span&gt;
        &lt;span&gt;{category.name}&lt;/span&gt;
      &lt;/div&gt;
    ))}
  &lt;/div&gt;
);

答案1

得分: 1

代码中的错误是地图循环中返回了2个子元素。解决这个问题的一种方法是使用React Fragment来包装这些组件。

https://reactjs.org/docs/fragments.html

// Sidebar.jsx
import { categories } from "./constants.js";

export const Sidebar = () => (
  <div>
    {categories.map((category) => (
      <React.Fragment>
        <span>{category.icon}</span>
        <span>{category.name}</span>
      </React.Fragment>
    ))}
  </div>
);

另外,你必须注意列表和循环中的key属性。

https://reactjs.org/docs/lists-and-keys.html

还有一点,将Icon的调用作为你的对象的值,然后将其作为React组件调用。

// Sidebar.jsx
import { categories } from "./constants.js";

export const Sidebar = () => (
  <div>
    {categories.map((category) => {
      const IconComponent = category.icon;
      return (
        <> {/* 使用片段的另一种方式 */}
          <span>
            <IconComponent />
          </span>
          <span>{category.name}</span>
        </>
      )}
    )}
  </div>
);

最后但同样重要的是,你的代码中存在语法错误。在循环的最后一行缺少一个}。

英文:

There is an error on the map loop.

You can't return 2 children in the map loop.
One way to solve it is using React Fragment to wrap up those components.

https://reactjs.org/docs/fragments.html

// Sidebar.jsx
import { categories } from &quot;./constants.js&quot;;

export const Sidebar = () =&gt; (
  &lt;div&gt;
    {categories.map((category) =&gt; (
      &lt;React.Fragment&gt;
        &lt;span&gt;{category.icon}&lt;/span&gt;
        &lt;span&gt;{category.name}&lt;/span&gt;
      &lt;/React.Fragment&gt;
    ))}
  &lt;/div&gt;
);

Another thing, you must be alert to the key prop inside lists and loops.

https://reactjs.org/docs/lists-and-keys.html

Third thing. Attach the Icon call as the value of your obj, and then call this as a react component

// Sidebar.jsx
import { categories } from &quot;./constants.js&quot;;

export const Sidebar = () =&gt; (
  &lt;div&gt;
    {categories.map((category) =&gt; {
      const IconComponent = category.icon;
      return (
        &lt;&gt; // another way to use fragment
          &lt;span&gt;
            &lt;IconComponent /&gt;
          &lt;/span&gt;
          &lt;span&gt;{category.name}&lt;/span&gt;
        &lt;/&gt;
      )}
    )}
  &lt;/div&gt;
);

Last but not least, there is a syntax error in your code. There is a } missing in the final line of the loop

答案2

得分: 0

你无法在 js 文件中包含 JSX,但你的图标是从 "react-icons/md" 导入的,并将它们用作 JSX 元素。

如果你想在 constants.js 中使用这些图标,请尝试将其重命名为 constants.jsx

英文:

You can't include JSX inside a js file but your icons are being imported from "react-icons/md" and you use them as JSX elements.

If you want to use these icons inside of constants.js try renaming it to constants.jsx

答案3

得分: 0

// data categories.js
export const categories = [
  { name: "新闻", icon: "主页" },
  { name: "编码", icon: "代码" },
  { name: "ReactJS", icon: "代码" },
];

// sidebar.js
import { categories } from "./constants.js";
import { MdHomeIcon, MdCodeIcon } from "react-icons/md";

export const Sidebar = () => (
  <div>
    {categories && categories.map((category, i) => (
      <div key={i}>
        {category.icon === "代码" ? <MdCodeIcon/> : <MdHomeIcon/>}
        <span>{category.name}</span>
        <div></div>
      </div>
    ))}
  </div>
);

请注意,我已经将代码中的HTML实体(如&quot;)翻译为相应的中文文本。如果您需要进一步的修改,请告诉我。

英文:
//data categories.js
export const categories = [
  { name: &quot;New&quot;, icon: &quot;Home&quot; },
  { name: &quot;Coding&quot;, icon: &quot;Code&quot; },
  { name: &quot;ReactJS&quot;, icon: &quot;Code&quot; },
];

    //sidebar.js
import { categories } from &quot;./constants.js&quot;;
import { MdHomeIcon, MdCodeIcon } from &quot;react-icons/md&quot;;


export const Sidebar = () =&gt; (
  &lt;div&gt;
    {categories &amp;&amp; categories.map((category, i) =&gt; (
      &lt;div key = {i}&gt;
      {category.icon === &quot;Code&quot;? &lt;MdCodeIcon/&gt; : &lt;MdHomeIcon/&gt;}
      &lt;span&gt;{category.name}&lt;/span&gt;
      &lt;div/&gt;
    ))}
  &lt;/div&gt;
);

you can edit the code as needed

huangapple
  • 本文由 发表于 2023年2月6日 21:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362125.html
匿名

发表评论

匿名网友

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

确定