简体   繁体   English

如何使用 React 中的条件在组件内渲染 3 个 SVG 中的 1 个?

[英]How do I render 1 of 3 SVGs inside a component using conditions in React?

Here is the code that I have now.这是我现在拥有的代码。 I can't figure this out.我想不通。 Trying to render an SVG icon based on the prop.尝试根据道具呈现 SVG 图标。 For example would render a house icon例如会渲染一个房子图标

import React from "react";


    function BuildingIcon() {
      return <img src="./building.svg" alt="building" />;
    }

    function HouseIcon() {
      return <img src="./house.svg" alt="house" />;
    }

    function LatlongIcon() {
      return <img src="./latlong.svg" alt="globe" />;
    }
    function Icon(props) {
      const iconStyle = props.iconStyle;
      switch (iconStyle) {
        case "building":
          return <BuildingIcon />;
        case "house":
          return <HouseIcon />;
        case "latlong":
          return <LatlongIcon />;
        default:
          return <HouseIcon />;
      }
    }

    export default Icon;

You could actually just import the images into your component, and them specifying them as the src value.您实际上可以将图像导入到您的组件中,然后它们将它们指定为src值。

import logo from "./lat-long.png";

In addition, there is no need to go 2 levels deep when you destructure them.此外,解构它们时无需深入 2 层。

In addition, you should check if iconStyle is properly passed down to your component, and that props.iconStyle is defined.此外,您应该检查iconStyle是否正确传递给您的组件,以及props.iconStyle是否已定义。

import React from "react";
import logo from "./lat-long.png";

function BuildingIcon() {
  return <img src={logo} alt="globe" />;
}

function HouseIcon() {
  return <img width="24px" src={logo}  alt="globe" />;
}

function LatlongIcon() {
  return <img src={logo}  alt="globe" />;
}

function Icon(props) {
  const { iconStyle } = props;
  switch (iconStyle) {
    case "building":
      return <BuildingIcon />;
    case "house":
      return <HouseIcon />;
    case "latlong":
      return <LatlongIcon />;
    default:
      return <HouseIcon />;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个 SVG 放在一个渲染中(React Native)? - How do I put multiple SVGs in one render (React Native)? 如何使用webpack直接在我的React组件中加载SVG? - How do I load SVGs directly in my React component using webpack? 如何使用 React 在组件渲染中禁用元素的动画? - How do I disable animation of a element in a component render using React? 如何将 React 组件渲染到主 App 组件中/ - How do I render a React component into the main App component/ 如何在 React 中的按钮上使用 SVG 作为背景图像 - How do I use SVGs for background images on buttons in React 如何根据多个条件设置 React 组件的样式? - How do I style a react component based on multiple conditions? 渲染 svgs 并在 React 组件中设置内部 css.this - render svgs and set inner css within a React component .this 在树枝条件下渲染组件 - Render react component with twig conditions 如何使用Material-UI AppBar onTitleClick属性在React中渲染新组件? - How do I Render a new component in React using Material-UI AppBar onTitleClick Property? 如何使用反应钩子将我的 class 和 render() 转换为功能组件? - How do I convert my class and render() to a functional component using react hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM