What is being passed to a method call? React Project "Authorization Form"

Hello,

On the react course, on the react components section, project Authorization From (link: https://www.codecademy.com/courses/react-101/projects/authorization-form)

We have a method in the project that is called from the onSubmit event of the form. When we call that method, we don’t pass any parameters to it. Yet, something is passed because the function knows what “e” is. I’m curious to what is this mechanism at play? What is it called and how does it work? Does Javascript always passes something on to functions when we call them? I’m confused by this.

authorize(e) {
const password = e.target.querySelector(
‘input[type=“password”]’).value;
const auth = password == this.state.password;
this.setState({
authorized: auth
});
}

Thank you for your help.

we don’t call this method/function. We say to Javascript: when the form is submitted, can you please execute this function?

so are passing the function by reference, then Javascript will call the function for us with the arguments.

simple example:

const onSubmit = (func) => {
  const event = {
    type: 'click',
  }
  func(event);
}

const authorize = (event) => {
  console.log(event.type);
}

onSubmit(authorize);
2 Likes

Hi,
certain actions trigger events. A click is an event.
When you have an onClick method, you can pass the fired event as an argument to the method. As a convention this is either passed in as e or event.
An event is represented by an object. An important property of the event object is target, for example.

1 Like