Skip to content

Instantly share code, notes, and snippets.

@yeisoncruz16
Created May 15, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeisoncruz16/9f132598fd942a3ea176e9904fe0f4c6 to your computer and use it in GitHub Desktop.
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.
import React from 'react';
export class Button extends React.Component {
render() {
return (
<button onClick={this.props.onClick}>
Click me!
</button>
);
}
}
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')
);
@yeisoncruz16
Copy link
Author

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 :

@yeisoncruz16
Copy link
Author

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