简体   繁体   English

React 组件中的内联 SVG 未显示

[英]Inline SVGs in React components not showing

I have an svg inside a React functional component:我在 React 功能组件中有一个 svg:

import React, { useEffect } from 'react';

import './logo.css';

function Logo({}) {


    const logo = () => (
        <svg width="130" height="144" viewBox="0 0 130 144">
            <path
                d="M59.1046 143.548V143.404C57.8398 143.404 56.575 143.404 55.3262 143.26L57.6316 134.486H59.0726C62.5047 134.488 65.926 134.101 69.2711 133.333V142.491C65.8988 143.135 62.4737 143.462 59.0405 143.468V143.612"
                fill="white"
            />
        </svg>
    );

    return { logo };
}

export default Logo;

This does not display anything when correctly imported and used in another component.当在另一个组件中正确导入和使用时,这不会显示任何内容。 I have other inline svgs in other components that are working fine.我在其他工作正常的组件中有其他内联 svg。

I try editing svgs in Illustrator and no matter how I save or export, the result is never compatible with React when inlining.我尝试在 Illustrator 中编辑 svgs,无论我如何保存或导出,在内联时结果都与 React 不兼容。 Here is an example of an svg path created in Illustrator that will not work inline in React:这是在 Illustrator 中创建的 svg 路径的示例,它不能在 React 中内联工作:

<path d="M103.42,120.08l-6.66-6.25a32.92,32.92,0,0,0,3.39-9.06l8.15,4.1A40.69,40.69,0,0,1,103.42,120.08Z"/>

What are the syntax requirements for inline React svgs?内联 React svg 的语法要求是什么? It is really useful to dynamically change svg properties and animate with GSAP, but I can't do that if the svgs will not work reliably?动态更改 svg 属性并使用 GSAP 制作动画真的很有用,但如果 svg 不能可靠地工作,我就不能这样做吗?

You have to invoke logo in Logo return statement.您必须在徽标返回语句中调用徽标。 If you return just the logo reference as in your code, then it will be not a valid React component.如果您只返回代码中的徽标引用,那么它将不是有效的 React 组件。

function Logo() {
  const logo = () => (
    <svg width="130" height="144" viewBox="0 0 130 144">
      <path
        d="M59.1046 143.548V143.404C57.8398 143.404 56.575 143.404 55.3262 143.26L57.6316 134.486H59.0726C62.5047 134.488 65.926 134.101 69.2711 133.333V142.491C65.8988 143.135 62.4737 143.462 59.0405 143.468V143.612"
        fill="white"
      />
    </svg>
  );

  return logo();
}

The svg is definitely there, but it needs some styling: https://codesandbox.io/s/heuristic-newton-m6js9 svg 肯定存在,但它需要一些样式: https://codesandbox.io/s/heuristic-newton-m6js9

It should be like return logo();它应该像return logo(); . . Because the logo here is a function and you need to invoke it like logo();因为这里的logo是一个 function 并且你需要像logo();

Calling just return logo will not work, here logo is a function not a variable.只调用return logo是行不通的,这里的logo是一个 function 而不是变量。 So the correct syntax to call a function is function_name followed by () .因此调用 function 的正确语法是function_name后跟()

Check the stackblitz implementation Stackblitz link检查 stackblitz 实现Stackblitz 链接

function Logo({}) {
  const logo = () => ( <svg width = "130"
    height = "144"
    viewBox = "0 0 130 144" >
    <path d = "M59.1046 143.548V143.404C57.8398 143.404 56.575 143.404 55.3262 143.26L57.6316 134.486H59.0726C62.5047 134.488 65.926 134.101 69.2711 133.333V142.491C65.8988 143.135 62.4737 143.462 59.0405 143.468V143.612"
    fill = "red" /
    >
    </svg>
  );

  return logo();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM