React Events

Handle clicks, input and form submissions.

✍️ 3 exercises + code challenge in this lesson

React events are written in camelCase and receive a function:

  • onClick={handleClick} (pass the function, do not call it)
  • onClick={() => remove(id)} to pass arguments
  • onChange, onSubmit, onMouseEnter, onKeyDown
  • The handler receives an event object; call e.preventDefault() to stop default behaviour.

Key Takeaways: React Events

Mastering React Events helps you write clean and reliable React code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

React Events Code Example

React
<div id="root"></div>
<script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"></script>

<script type="text/babel">
const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;
function App() {
  function sayHello(name) {
    alert("Hello, " + name + "!");
  }

  return (
    <div>
      <button onClick={() => sayHello("Ali")}>Greet Ali</button>{" "}
      <button onDoubleClick={() => alert("Double clicked!")}>Double-click me</button>
      <p onMouseEnter={(e) => (e.target.style.color = "#149ECA")}>Hover to colour me</p>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Free sign-in required · Google login available

React Events Practice Exercises

Check what you learned in “React Events”.

/ 3 solved
Fill in the blank

React event names are camelCase.

<button ={handle}>Save</button>
✓ Correct! Not quite. Try again.
Multiple choice

How do you pass an argument to an event handler?

✓ Correct! Not quite. Try again.
Code challenge

Clicking the button should change the <p> text from "Off" to "On". (Use state; the check clicks the button.)

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services