Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React & SVG: How do I make <path> support onClick?

This is what React SVG currently supports: http://facebook.github.io/react/docs/tags-and-attributes.html#svg-attributes

I'm trying to figure out how to make a shape I drew using the SVG path clickable. If there is another way to draw a shape that can be made clickable, that works too.

Thanks!

like image 295
achalla Avatar asked Nov 23 '15 18:11

achalla


People also ask

What is React used for?

React is a JavaScript library developed by Facebook which, among other things, was used to build Instagram.com. Its aim is to allow developers to easily create fast user interfaces for websites and applications alike. The main concept of React. js is virtual DOM.

Is React for HTML or js?

To get an overview of what React is, you can write React code directly in HTML. But in order to use React in production, you need npm and Node. js installed.

What is in React means?

1 : to exert a reciprocal or counteracting force or influence —often used with on or upon. 2 : to change in response to a stimulus. 3 : to act in opposition to a force or influence —usually used with against. 4 : to move or tend in a reverse direction. 5 : to undergo chemical reaction.

Is React better or Angular?

React is better than Angular due to it's virtual DOM implementation and rendering optimizations. Migrating between React's versions is quite easy, too; you don't need to install updates one by one, as in the case of Angular. Finally, with React, developers have myriads of existing solutions they can use.


1 Answers

I wrap my SVG with a div and apply any attributes that I desire (click handlers, fill colors, classes, width, etc..), like so (fiddle link):

import React, { PropTypes } from 'react'

function XMark({ width, height, fill, onClick }) {
    return (
        <div className="xmark-container" onClick={onClick}>
            <svg className='xmark' viewBox="67 8 8 8" width={width} height={height} version="1.1" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink">
                <polygon stroke="none" fill={fill} fillRule="evenodd" points="74.0856176 9.4287633 71.5143809 12 74.0856176 14.5712367 73.5712367 15.0856176 71 12.5143809 68.4287633 15.0856176 67.9143824 14.5712367 70.4856191 12 67.9143824 9.4287633 68.4287633 8.91438245 71 11.4856191 73.5712367 8.91438245 74.0856176 9.4287633 74.0856176 9.4287633 74.0856176 9.4287633" />
            </svg>
        </div>
    )
}
XMark.propTypes = {
    width: PropTypes.number,
    height: PropTypes.number,
    fill: PropTypes.string,
    onClick: PropTypes.func,
}
XMark.defaultProps = {
    width: 8,
    height: 8,
    fill: '#979797',
    onClick: null,
}
export default XMark

You can of course ditch the wrapper and apply the onClick to the svg element as well, but I've found this approach works well for me! (I also try and use pure functions when possible https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc)

like image 138
jacoballenwood Avatar answered Sep 21 '22 14:09

jacoballenwood