最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reactjs Invalid DOM property error on SVG elements - Stack Overflow

programmeradmin1浏览0评论

I am working on code refactoring on a React project. It has ponents like this

import * as React from "react";

function SvgPage(props) {
  return (
    <svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  </svg> );
}



export default SvgPage;

I am working on code refactoring on a React project. It has ponents like this

import * as React from "react";

function SvgPage(props) {
  return (
    <svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  </svg> );
}



export default SvgPage;

These SVG files render without any issue but in the console, it gives me the error

Warning: Invalid DOM property stroke-width. Did you mean strokeWidth?

How do I get rid of these console warnings? I search google and find SVGR library. But I am not sure how to use it to fix the problem.

any help!

Thanks in advance! =)

Share Improve this question asked Mar 18, 2021 at 6:01 margherita pizzamargherita pizza 7,17529 gold badges102 silver badges178 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 10

From the docs,

Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.

For example, class bees className in JSX, and tabindex bees tabIndex.

Replace hyphenated prop names with their camelCase versions.

<circle cx="50" cy="50" r="40" stroke="green" strokeWidth="4" fill="yellow" />

I think this is your error

so you have to change attributes inside the svg tag to use camel case, like stroke-width="4" as strokeWidth="4" and stroke-linecap="" as strokeLinecap=""

发布评论

评论列表(0)

  1. 暂无评论