Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent text selection on double click in a React component

I have a react component that displays some text inside a div. I also have a double click handler on the div. Everything works fine except the text is shown as selected when the user double clicks on it. This is to be expected, but I want to prevent this happening.

I tried to use event.preventDefault() but it made no difference. Any ideas?

var Example = React.createClass({
    toggle: function(e) {
        e.preventDefault();
    },

    render: function() {
        return (
            <div onDoubleClick={this.toggle}>
                Example text!
            </div>
        );
    }
});
like image 690
Phil Wright Avatar asked Oct 20 '25 06:10

Phil Wright


1 Answers

Try adding userSelect: 'none' -

<div onDoubleClick={this.toggle} style={{userSelect: 'none'}}>
    Example text!
</div>
like image 147
Brian Burns Avatar answered Oct 21 '25 19:10

Brian Burns



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!