Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactjs Invalid DOM property error on SVG elements

I am working on code refactoring on a React project. It has components 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! =)

like image 277
Pathum Kalhan Avatar asked Jul 22 '26 01:07

Pathum Kalhan


1 Answers

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 becomes className in JSX, and tabindex becomes tabIndex.

Replace hyphenated prop names with their camelCase versions.

<circle cx="50" cy="50" r="40" stroke="green" strokeWidth="4" fill="yellow" />
like image 129
Arun Kumar Mohan Avatar answered Jul 24 '26 15:07

Arun Kumar Mohan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!