Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

Keyboard and Mouse Events in React

Learn how React responds to keyboard actions and mouse interactions with simple examples and practical outputs.

1. What Are Keyboard and Mouse Events?

Keyboard events occur when a user presses or releases a key. Mouse events occur when a user clicks, moves the pointer, or interacts with an element using the mouse.

Common keyboard events
  • onKeyDown — when a key is pressed.
  • onKeyUp — when a key is released.
Common mouse events
  • onClick — when an element is clicked.
  • onDoubleClick — when an element is double-clicked.
  • onMouseEnter — when the pointer enters an element.
  • onMouseLeave — when the pointer leaves an element.
  • onMouseMove — when the pointer moves over an element.

2. Keyboard Events: onKeyDown and onKeyUp

Use onKeyDown to respond when a key is pressed and onKeyUp to respond when a key is released.

KeyboardEvents.jsx JSX
function KeyboardEvents() {
    function handleKeyDown(event) {
        console.log("Key pressed: " + event.key);
    }

    function handleKeyUp(event) {
        console.log("Key released: " + event.key);
    }

    return (
        <input
            onKeyDown={handleKeyDown}
            onKeyUp={handleKeyUp}
            placeholder="Type something"
        />
    );
}

export default KeyboardEvents;
Simple Explanation: event.key tells us which key the user pressed or released. For example, pressing the letter A gives "a" in most standard cases.
Interactive Keyboard Output

Key pressed: None

Key released: None

3. Detecting the Enter Key

Sometimes we want to perform an action only when the user presses a particular key, such as Enter.

EnterKey.jsx JSX
function EnterKey() {
    function handleKeyDown(event) {
        if (event.key === "Enter") {
            alert("Enter key pressed!");
        }
    }

    return (
        <input
            onKeyDown={handleKeyDown}
            placeholder="Press Enter"
        />
    );
}

export default EnterKey;
Remember: Use event.key === "Enter" to check whether the Enter key was pressed.
Try It Yourself
Waiting for Enter key...

4. Mouse Click Event: onClick

The onClick event runs when the user clicks an element. It is commonly used for buttons and actions.

MouseClick.jsx JSX
function MouseClick() {
    function handleClick() {
        alert("Button clicked!");
    }

    return (
        <button onClick={handleClick}>
            Click Me
        </button>
    );
}

export default MouseClick;
Simple Explanation: When the button is clicked, React calls handleClick().
Interactive Mouse Click
Button clicked: 0 times

5. Mouse Enter and Leave Events

Use onMouseEnter when the pointer enters an element and onMouseLeave when it leaves. These events are useful for hover effects and interactive cards.

MouseHover.jsx JSX
function MouseHover() {
    function handleMouseEnter() {
        console.log("Mouse entered");
    }

    function handleMouseLeave() {
        console.log("Mouse left");
    }

    return (
        <div
            onMouseEnter={handleMouseEnter}
            onMouseLeave={handleMouseLeave}>
            Move your mouse here
        </div>
    );
}

export default MouseHover;
Interactive Hover Area
Move your mouse here

Move the pointer into or out of this box.

Pointer has not entered the box yet.

6. Mouse Double-Click Event

The onDoubleClick event runs when a user double-clicks an element.

DoubleClick.jsx JSX
function DoubleClick() {
    function handleDoubleClick() {
        alert("Element double-clicked!");
    }

    return (
        <button onDoubleClick={handleDoubleClick}>
            Double Click Me
        </button>
    );
}

export default DoubleClick;
Interactive Double-Click
Double-click the button to see the output.

7. Mouse Move Event

The onMouseMove event runs when the pointer moves over an element. It can be used to track pointer coordinates or create interactive effects.

MouseMove.jsx JSX
function MouseMove() {
    function handleMouseMove(event) {
        console.log(
            "X: " + event.clientX +
            ", Y: " + event.clientY
        );
    }

    return (
        <div onMouseMove={handleMouseMove}>
            Move your mouse here
        </div>
    );
}

export default MouseMove;
Interactive Mouse Coordinates
Move your mouse inside this area

X coordinate: -

Y coordinate: -

8. Difference Between Keyboard and Mouse Events

Event Purpose Example
onKeyDown Runs when a key is pressed. Detect Enter key.
onKeyUp Runs when a key is released. Detect released key.
onClick Runs when an element is clicked. Button action.
onDoubleClick Runs on a double-click. Open an item.
onMouseEnter Runs when the pointer enters. Hover effect.
onMouseLeave Runs when the pointer leaves. Remove hover effect.
onMouseMove Runs as the pointer moves. Track coordinates.

9. Practice Questions

  1. What are keyboard events in React?
  2. What is the difference between onKeyDown and onKeyUp?
  3. How can you detect the Enter key?
  4. What is the purpose of onClick?
  5. Explain onMouseEnter and onMouseLeave.
  6. Create a button that counts clicks.
  7. Create an input that displays the last pressed key.
  8. Create a box that displays mouse coordinates.

Summary

Keyboard events help React respond to key presses and releases. Mouse events respond to clicks, double-clicks, pointer entry, pointer exit, and pointer movement. Use event handlers such as onKeyDown, onKeyUp, onClick, onMouseEnter, and onMouseMove to build interactive components.