Simulate click event on react element

后端 未结 7 1968
孤独总比滥情好
孤独总比滥情好 2020-12-03 03:06

I\'m trying to simulate a .click() event on a React element but I can\'t figure out why it is not working (It\'s not reacting when I\'m firing the

7条回答
  •  春和景丽
    2020-12-03 03:47

    Using React useRef Hooks you can trigger a click event on any button like this:

    export default const () => {
    
        // Defining the ref constant variable
        const inputRef = React.useRef(null);
    
        // example use
        const keyboardEvent = () => {
          inputRef.current.handleClick(); //Trigger click
        }
    
        // registering the ref
        return (
           
    console.log('clicked')}> hello
    ) }

提交回复
热议问题