FAQ: this.props - Put an Event Handler in a Component Class

This community-built FAQ covers the “Put an Event Handler in a Component Class” exercise from the lesson “this.props”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn ReactJS: Part I

FAQs on the exercise Put an Event Handler in a Component Class

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why the guide, in point 1, stands “Don’t forget to NOT separate talk and render with a comma.”.

What would happen if I put a comma or a semicolon between methods in a React component?

4 Likes

In this exercise is stated:

There’s a problem: talk is defined outside of the Talker component class. That’s not how we do things here in React-land!
Rewrite talk , so that it is a method defined in the definition of Talker

This and the following exercises (Pass an Event Handler as a prop, Receive an Event Handler as a prop) want us to create these two JS files:

//button.js - defining Button component to be exported 
import React from 'react';
export class Button extends React.Component {
  render() {
    return (
      <button onClick={this.props.talk}>
        Click me!
      </button>
    );
  }
}
//talker.js - defining talker() function, Talker component and importing Button component
import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from './Button';

function 
class Talker extends React.Component {
  talk() {
    let speech = '';
    for (let i = 0; i < 10000; i++) {
      speech += 'blah ';
    }
    alert(speech);
  };
  render() {
    return <Button talk={this.talk} />;
  }
};

ReactDOM.render(
  <Talker />,
  document.getElementById('app')
);

However I don’t understand what is the reason why we should have the function talk() inside the Talker component.
What does the sentence “That’s not how we do things here in React-land!” mean exactly?

Personally I would have kept separated the logic of the talk() function and the Talker component, writing the talker.js file this way:

//talker.js - defining talker() function, Talker component and importing Button component
import React from 'react';
import ReactDOM from 'react-dom';
import {Button} from './Button';
function talk() {
  let speech = '';
  for (let i = 0; i < 10000; i++) {
    speech += 'blah ';
  }
  alert(speech);
};
class Talker extends React.Component {
  render() {
    return <Button talk={talk} />;
  }
};
ReactDOM.render(
  <Talker />,
  document.getElementById('app')
);

Is there a reason why the function talk() should be defined inside the class Talker, other than inheritance?
What is the advantage to have this function be inheritable by Talker instances?

1 Like

Why is it “function talk() {}” outside of class and just “talk() {}” inside the class?
Why is the word "function"not used every time?

Because inside a class a function is a class method and has a similar syntax to when you declare an object property

JavaScript supports the concept of classes as a syntax for creating objects.

Cheers

1 Like

For anyone interested:
the reason why we put talk() inside the Talker class Component instead of keeping it outside in the global scope of the file Talker.js is because one of the main uses of Components is to be modular, thus Talker is likely to be exported to be used somewhere else, but if we export Talker then the reference to the function talk() would be lost, unless it is also exported separately.

However it could also be speculated that keeping the function talk() separated from Talker would be just like a separation of concerns: Functional and Presentational respectively, talk() could be converted to a Component and live in a separate file (talkerContainer.js) and be a Container for Talker.
In this case Talker would be exported and then imported by talkerContainer.js and used there.

Containers are explained in one of the last lessons of the React course.

Cheers,

2 Likes

Thank you. Great explanation about modularity.

However, I like @moisesrj7160057532 and others have no idea why we can’t put a semicolon or comma to separate the methods, talk( ) and render ( ) from inside the component. Do you perhaps know why?

Thank you for the appreciation. Sure, the reason is similar to the answer to teemu9 in this thread.

First: It’s not something specific to React Components. The declaration of a Javascript Class follows a similar syntax to that of Objects, but still differs in some things, as for Objects: each key is separated with a comma, in a Class body doing this would throw a SyntaxError.

Some examples:

This Class is written with the correct syntax and creating a new instance gives back some info:
image_2021-05-09_012900

This Class is written using the same syntax for Objects, using a comma, it throws an immediate error:
image_2021-05-09_013234

This Class is written using the common syntax of separating code blocks with a semicolon, interestingly enough it is accepted and creating a new instance gives us some info:
image_2021-05-09_013510

Ignore the const declaration of mariaHobby, as I used the first class Hobby, instead of the second one Hobbyt.

1 Like

Thank you for the prompt reply. I’m not next to my computer but I’ll test if semicolons do work. If they work…then great.

Although semicolons may seem unnecessary at times, they do ease the search process for start and end points of declarations.

You’re welcome. I would get used however to not use semicolons all together inside a Class though.
I did and it is a good reminder I’m inside a Class and not somewhere else in the code. :slight_smile:

1 Like

First, I tried to move the entire Talk function into the Talker class, including “function” at the beginning. That threw an error. Removing “Function” solved it. Why is that?

It isn’t specific to React. It has to do with the syntax for class methods in JavaScript.

1 Like