Event Handling
● Event handling allows the user to interact with a webpage and do something specific
when a certain event like a click or a hover happens.
● When the user interacts with the application, events are fired, such as mouseover, key
press, change event, etc.
● The application must handle events and execute the code. In short, events are the actions
to which javascript can respond.
● The actions to which JavaScript can respond are called events.
● Handling events with react is very similar to handling events in DOM elements.
● Below are some general events that you would see in and out when dealing with
React-based websites:
● Clicking an element
● Submitting a form
● Scrolling page
● Hovering an element
● Loading a webpage
● Input field change
● User stroking a key
● Image loading
Handling events with react is very similar to handling events in DOM elements, although some
syntactic differences exist.
○ React events are written in camelCase.
○ A function is passed as the event handler rather than string.
The way to write events in html / DOM is below:
<button onclick=”handleClick()”>click me</button>
onclick is written in lower case in html as shown above and what action to take when this onclick
event triggers is taken care of by handleClick.
In React, events are named using camel case and you pass a function as event handler as shown
below:
Like in a functional component, event is written like below:
● Event handling allows the user to interact with a webpage and do something specific
when a certain event like a click or a hover happens.
● When the user interacts with the application, events are fired, such as mouseover, key
press, change event, etc.
● The application must handle events and execute the code. In short, events are the actions
to which javascript can respond.
● The actions to which JavaScript can respond are called events.
● Handling events with react is very similar to handling events in DOM elements.
● Below are some general events that you would see in and out when dealing with
React-based websites:
● Clicking an element
● Submitting a form
● Scrolling page
● Hovering an element
● Loading a webpage
● Input field change
● User stroking a key
● Image loading
Handling events with react is very similar to handling events in DOM elements, although some
syntactic differences exist.
○ React events are written in camelCase.
○ A function is passed as the event handler rather than string.
The way to write events in html / DOM is below:
<button onclick=”handleClick()”>click me</button>
onclick is written in lower case in html as shown above and what action to take when this onclick
event triggers is taken care of by handleClick.
In React, events are named using camel case and you pass a function as event handler as shown
below:
Like in a functional component, event is written like below: