Handling Events on React

Last Updated on June 17, 2024

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import {useState} from "react";
function Counter() {
const [count, setCount] = useState(0)
const buttonClicked = () => {
setCount(prevState => prevState + 1)
};
return (
<div>
<div>Counter Value : {count}</div>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={buttonClicked}>Next
</button>
</div>
);
}
export default Counter;
import {useState} from "react"; function Counter() { const [count, setCount] = useState(0) const buttonClicked = () => { setCount(prevState => prevState + 1) }; return ( <div> <div>Counter Value : {count}</div> <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={buttonClicked}>Next </button> </div> ); } export default Counter;
import {useState} from "react";

function Counter() {
    const [count, setCount] = useState(0)

    const buttonClicked = () => {
        setCount(prevState => prevState + 1)
    };

    return (
        <div>
            <div>Counter Value : {count}</div>
            <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
                    onClick={buttonClicked}>Next
            </button>
        </div>
    );
}

export default Counter;

Passing Arguments to Event Handlers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import {useState} from "react";
function Counter() {
const [count, setCount] = useState(0)
const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => {
console.log(e);
setCount(prevState => prevState + 1)
};
return (
<div>
<div>Counter Value : {count}</div>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={(e) => buttonClicked(e)}>Next
</button>
</div>
);
}
export default Counter;
import {useState} from "react"; function Counter() { const [count, setCount] = useState(0) const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => { console.log(e); setCount(prevState => prevState + 1) }; return ( <div> <div>Counter Value : {count}</div> <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={(e) => buttonClicked(e)}>Next </button> </div> ); } export default Counter;
import {useState} from "react";

function Counter() {
    const [count, setCount] = useState(0)

    const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => {
        console.log(e);
        setCount(prevState => prevState + 1)
    };

    return (
        <div>
            <div>Counter Value : {count}</div>
            <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
                    onClick={(e) => buttonClicked(e)}>Next
            </button>
        </div>
    );
}

export default Counter;

Prevent default behavior

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import {useState} from "react";
function Counter() {
const [count, setCount] = useState(0)
const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => {
if (e.cancelable)
{
e.preventDefault();
}
setCount(prevState => prevState + 1)
};
return (
<div>
<div>Counter Value : {count}</div>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={(e) => buttonClicked(e)}>Next
</button>
</div>
);
}
export default Counter;
import {useState} from "react"; function Counter() { const [count, setCount] = useState(0) const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => { if (e.cancelable) { e.preventDefault(); } setCount(prevState => prevState + 1) }; return ( <div> <div>Counter Value : {count}</div> <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={(e) => buttonClicked(e)}>Next </button> </div> ); } export default Counter;
import {useState} from "react";

function Counter() {
    const [count, setCount] = useState(0)

    const buttonClicked = (e: React.MouseEvent<HTMLButtonElement>) => {
        if (e.cancelable)
        {
            e.preventDefault();
        }

        setCount(prevState => prevState + 1)
    };

    return (
        <div>
            <div>Counter Value : {count}</div>
            <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
                    onClick={(e) => buttonClicked(e)}>Next
            </button>
        </div>
    );
}

export default Counter;

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.

For example, this can be useful when:

  • Clicking on a “Submit” button, prevent it from submitting a form
  • Clicking on a link, prevent the link from following the URL

Note: Not all events are cancelable. Use the cancelable property to find out if an event is cancelable.

Note: The preventDefault() method does not prevent further propagation of an event through the DOM. Use the stopPropagation() method to handle this.

References
https://reactjs.org/docs/handling-events.html
https://www.w3schools.com/jsref/event_preventdefault.asp
https://stackoverflow.com/questions/32298064/preventdefault-not-working-in-on-input-function