React. preventDefault() for onCopy event does not work

前端 未结 3 1510
青春惊慌失措
青春惊慌失措 2021-02-13 21:21

I\'m trying to figure out how to make the clipboard events return false on the onCopy event. I use for test the onCopy handler and e.preventDefau

3条回答
  •  我在风中等你
    2021-02-13 22:07

    Above solution which is mentioned doesn't seems working for me, but if talk about counter values in state its getting managed properly by writing handlerCopy in below manner(Updation of state values).

    import React from 'react';
    import ReactDOM from 'react-dom';
    import PropTypes from 'prop-types';
    import ReactDOMServer from 'react-dom/server';
    
    class App extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          time: '',
          timer: false,
          counter: 0
        };
      }
    
       handlerCopy(e) {
        var val = this.state.counter;
        val = val + 1;
        this.setState({
          counter: val
        }, function(){
          console.log('new acounter:- ', this.state.counter);
        })
        alert('Don\'t copy it!');
      }
    
    
      render() {
        return (
          
            

    this.handlerCopy(e)}>Copy me!

    Copy count: {this.state.counter}

    ); } } document.addEventListener('click', function(e) { console.log('propagation',e) }, false); export default App;

    This handlerCopy function mentioned above is not making any changes for me @Max Wolfen

     handlerCopy(e) {
        console.log(e.target.innerHTML);
        e.preventDefault();
        e.nativeEvent.stopImmediatePropagation();
    
        this.setState(prevState => ({
          counter: prevState.counter + 1
        }));
    
        alert('Don\'t copy it!');
      }
    

提交回复
热议问题