How can I place text inside an SVG circle with React JS?

12,748

A circle is a layer, and so is a Text node. You have to have them as separate layers and make them look as if they belong together:

<SVGComponent height="110" width="500">
    <Circle cx="50" cy="55" r="45" fill="none" stroke="#F0CE01" strokeWidth="4" />
    <text textAnchor="middle" x="250" y="55">Circle Text</text>
</SVGComponent>
Share:
12,748
D'Arcy Rail-Ip
Author by

D'Arcy Rail-Ip

VP of Technology @ CineSend. Message me if you're looking for a job!

Updated on July 01, 2022

Comments

  • D'Arcy Rail-Ip
    D'Arcy Rail-Ip almost 2 years

    I have the following code:

    import React from 'react';
    
    var SVGComponent = React.createClass({
        render: function() {
            return <svg {...this.props}>{this.props.children}</svg>;
        }
    });
    
    var Circle = React.createClass({
        render: function() {
            return <circle {...this.props}>{this.props.children}</circle>;
        }
    });
    
    var MakeCircles = React.createClass({
    
        render: function () {
            return(
                <div>
                    <SVGComponent height="110" width="500">
                        <Circle 
                            cx="50" cy="55" r="45"
                            fill="none"
                            stroke="#F0CE01" strokeWidth="4" />     
                    </SVGComponent>
                </div>
            );
        }
    });
    export default MakeCircles
    

    I'm trying to get some text inside the circle but finding it absolutely difficult. Is there something/some add-on that can help me with this?

  • ArneHugo
    ArneHugo about 8 years
    Tiny thing: text-anchor should be textAnchor in React.
  • harshikerfuffle
    harshikerfuffle over 3 years
    this didn't work for me.. only the text renders <div height="110" width="500"> <circle cx="50" cy="55" r="45" fill="none" stroke="#F0CE01" strokeWidth="4" /> <text textAnchor="middle" x="250" y="55"> Circle Text</text> </div>