Conditional Rendering in React Complete Guide with Examples 2026

Image
  Conditional Rendering in React Conditional Rendering is one of the most useful concepts in React. It means showing different content on the screen depending on a condition. For example: If a user is logged in → show Logout If a user is not logged in → show Login If a student has passed → show Congratulations If a product is available → show Buy Now If data is loading → show Loading... React does not have a separate if rendering tag. Instead, we use normal JavaScript conditions such as if, the ternary operator, &&, and switch to decide what should appear in the UI.

Keyboard Events in React – Complete Beginner's Guide (2026)

What are Keyboard Events in React?

What are Keyboard Events in React? Examples & Syntax


Keyboard Events in React are special events that occur whenever a user interacts with the keyboard while using an application. These events allow developers to detect which key was pressed, released, or held down and perform specific actions based on that input.

For example, when a user types inside a search box, presses Enter to submit a form, or uses shortcut keys like Ctrl + S, React can capture these actions using keyboard events.

Keyboard events are commonly used to improve user experience by making applications faster, more interactive, and easier to use without relying only on mouse clicks.

What are mouse events in react

Why are Keyboard Events Important?

Keyboard events play an important role in modern web applications because many users prefer keyboard shortcuts instead of repeatedly using the mouse.

They help developers:

  • Create faster user interactions
  • Improve accessibility
  • Build keyboard shortcuts
  • Validate user input
  • Submit forms quickly
  • Control games and interactive applications
  • Navigate menus using arrow keys

Without keyboard events, many advanced web application features would not be possible.

what is event handling in react

Real-Life Example

Imagine you are using Google Search.

  • You type your query.
  • Press Enter.
  • Search results appear instantly.

This entire process depends on keyboard events.

Another example is WhatsApp Web.

  • Type a message.
  • Press Enter.
  • The message is sent immediately.

This is another practical use of keyboard events.

Common Keyboard Events in React

React provides several keyboard event handlers.

Event

Description

onKeyDown

Fires when a key is pressed.

onKeyUp

Fires when the pressed key is released.

onKeyPress (Deprecated)

Previously fired when a printable key was pressed. It is no longer recommended.

Today, developers mainly use:

  • onKeyDown
  • onKeyUp

Keyboard Event Flow

User Presses a Key

       ⬇

Browser Detects Key

        ⬇

React Event Handler Executes

        ⬇

Function Runs

        ⬇

UI Updates

Keyboard Events in React Syntax

onKeyDown

<input onKeyDown={handleKeyDown} />

Keyboard Event in React onKeyUp

<input onKeyUp={handleKeyUp} />


Example 1 – Detect Pressed Key

function App() {

   function handleKey(event) {

    alert("You pressed: " + event.key);

  }

   return (

    <input

      type="text"

      placeholder="Type here..."

      onKeyDown={handleKey}

    />

  );

}

 export default App;

Keyboard Events in React Output

Type: Hello

 

Press A

 

Alert:

 

You pressed: A

How This Program Works

Step 1

The input field waits for keyboard input.

 ⬇

Step 2

The user presses a key.

 ⬇

Step 3

React triggers the onKeyDown event.

 ⬇

Step 4

The event object stores information about the pressed key.

 ⬇

Step 5

The function displays the key name.

Example 2 – Submit Using Enter Key

function App() {

   function checkKey(event) {

     if(event.key === "Enter"){

      alert("Form Submitted");

    }

   }

   return (

    <input

      type="text"

      placeholder="Enter Name"

      onKeyDown={checkKey}

    />

  );

}

 export default App;

Submit Using Enter Key Output

Enter Name

 

Press Enter

 

 

Form Submitted

Reusable components in react

Example 3 – Show Typed Keys

function App() {

   function display(event) {

    console.log(event.key);

  } 

  return (

    <input

      type="text"

      onKeyUp={display}

    />

  );

}

 export default App;


Console Output

H

e

l

l

o


Keyboard Event Object

Whenever a keyboard event occurs, React passes an event object that contains useful information about the key that was pressed.

Some commonly used properties include:

Property

Description

event.key

Returns the key value (e.g., "A", "Enter").

event.code

Returns the physical keyboard key (e.g., "KeyA").

event.ctrlKey

Indicates whether the Ctrl key is pressed.

event.shiftKey

Indicates whether the Shift key is pressed.

event.altKey

Indicates whether the Alt key is pressed.

event.metaKey

Indicates whether the Meta/Command key is pressed.


Detect Shortcut Keys

function App() {

   function shortcut(event){

     if(event.ctrlKey && event.key==="s"){

       event.preventDefault();

       alert("Save Shortcut");

    }

 

  }

 

  return(

    <input

      onKeyDown={shortcut}

      placeholder="Press Ctrl + S"

    />

  );

 

}

 

export default App;

Keyboard Events in React  Flowchart

Start

 

User Presses Key

   

React Receives Event

  

Check Which Key Was Pressed

  ⬇

Run Event Function

  ⬇

Update UI / Execute Logic

   

End

Keyboard Events in React  Diagram

        Keyboard

            

      React Input Box

           

      Keyboard Event

           

 Event Handler Function

           

 Business Logic

            

 Update Screen

Advantages of Keyboard Events

  • Makes applications more interactive.
  • Improves user productivity through shortcuts.
  • Enhances accessibility for keyboard-only users.
  • Simplifies form submission using the Enter key.
  • Helps validate input while typing.
  • Supports real-time search and filtering.
  • Enables game controls and interactive experiences.
  • Reduces dependence on mouse interactions.

Keyboard Events in React  Disadvantages

  • Excessive keyboard shortcuts can confuse users.
  • Different operating systems may use different shortcut conventions.
  • Overusing key events can make code harder to maintain.
  • Developers should avoid blocking standard browser shortcuts unless necessary.

Keyboard Events in React  Best Practices

  • Prefer onKeyDown and onKeyUp over onKeyPress.
  • Use event.key for readable key detection.
  • Avoid overriding common browser shortcuts without a good reason.
  • Keep event handlers focused on a single responsibility.
  • Test keyboard interactions across browsers and devices.
  • Consider accessibility so users can navigate without a mouse.

Keyboard Events in React  Interview Questions

1. What are Keyboard Events in React?

Keyboard Events allow React applications to detect and respond to keyboard interactions such as pressing, releasing, or holding keys.

2. Which keyboard event is most commonly used?

onKeyDown

3. Which keyboard event is deprecated?

onKeyPress

4. What does event.key return?

It returns the value of the key that the user pressed, such as "Enter" or "A".

5. How do you detect the Enter key?

if(event.key==="Enter")

6. Which event fires after releasing a key?

onKeyUp

7. Which property checks if the Ctrl key is pressed?

event.ctrlKey

8. Can React detect multiple keys together?

Yes. For example:

if(event.ctrlKey && event.key==="s")

Keyboard Events in React  Frequently Asked Questions (FAQs)

Q1. What are Keyboard Events in React?
Keyboard Events are event handlers that respond when users press or release keys on the keyboard.

Q2. What is the difference between onKeyDown and onKeyUp?
onKeyDown fires when a key is pressed, while onKeyUp fires when the key is released.

Q3. Why is onKeyPress not recommended?
It is deprecated because it behaves inconsistently across browsers and does not support all keys. onKeyDown and onKeyUp are the recommended alternatives.

Q4. How can I detect the Enter key?
Use event.key === "Enter" inside an onKeyDown or onKeyUp handler.

Q5. Can I create keyboard shortcuts in React?
Yes. You can combine modifier keys such as Ctrl, Shift, or Alt with event.key to implement shortcuts like Ctrl + S or Ctrl + Enter.

Keyboard Events in React  Conclusion

Keyboard Events are an essential feature of React that allow applications to respond instantly to user keyboard interactions. By using onKeyDown and onKeyUp, developers can implement features such as form submission, keyboard shortcuts, input validation, navigation, and interactive controls. Understanding how keyboard events and the event object work helps you build applications that are faster, more accessible, and easier to use.

 

Comments

Popular posts from this blog

HTML Tag

CSS Text Color Explained with Syntax and HTML Examples

HTML Input Type Submit Syntax and Example