Created
May 15, 2021 15:03
-
-
Save yeisoncruz16/9f132598fd942a3ea176e9904fe0f4c6 to your computer and use it in GitHub Desktop.
ReactJS onClick acts as event but only if they’re used on HTML-like elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { Button } from './Button'; | |
class Talker extends React.Component { | |
handleClick() { | |
let speech = ''; | |
for (let i = 0; i < 10000; i++) { | |
speech += 'blah '; | |
} | |
alert(speech); | |
} | |
render() { | |
return <Button onClick={this.handleClick} />; | |
} | |
} | |
ReactDOM.render( | |
<Talker />, | |
document.getElementById('app') | |
); |
Now look at Talker.js. Here, when you give an attribute named onClick, then the name onClick doesn’t do anything special. The name onClick does not create an event listener when used on - it’s just an arbitrary attribute name:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look at Button.js. When you give a an attribute named onClick, then the name onClick has special meaning. As you’ve learned, this special onClick attribute creates an event listener, listening for clicks on the :