1 of 11

React Events

2 of 11

What is an event in javascript/html?

3 of 11

Any time a user interacts with the web page, an event occurs

  • Click
  • Move mouse
  • Type text

4 of 11

How do I tell the web page what to do when I click this button?

<button>Click Me!</button>

5 of 11

Does this look familiar?

<button>Click Me!</button>

6 of 11

This is the normal built in way in javascript/html to tell a button what to do when it gets clicked. This has been around for 20 years.

How do we do this in React/JSX/

7 of 11

React uses the same syntax, why would they do it any different? A few rules

  • onClick must be camel cased
  • Remember we are in jsx so we must use {} for the javascript part
  • For events, the thing that always goes in the {} is a function

<button onClick={()=>console.log('hello')}>Click Me!</button>

8 of 11

One more example

What is e?

<input onChange={

(e)=>{

//what happens here

}

}>

</input>

9 of 11

e is an optional parameter. It is an object with information about the event.

target is the thing that the event happened on.

value is a property of a text box to get the words that are in the text box.

<input onChange={

(e)=>{

console.log(e.target.value)

}

}>

</input>

10 of 11

That is is. All you have to do is look up what other events there are and use them as you need.

11 of 11

Most commonly used

Button = onClick

Textbox = onChange

Drop down = onChange