1

For example, in a rendering REACT component, if I needed to pass in an argument it would look like this:

<button onClick={() => this.doSomething(**passed in parameter**)}>Press me!</button>

This works fine. But how come you don't have to clarify it as the parameter in the fat arrow function first? Like this:

<button onClick={(**PARAMETER PASSED IN HERE FIRST**) => this.doSomething(**SAME PARAMETER PLACED HERE**)}>Press me!</button>
4
  • 2
    Because your param is globally scoped? Commented Sep 19, 2019 at 18:58
  • Remember, JSX is a superset of JavaScript, so the same rules apply, with a little more syntax sprinkled on top! Commented Sep 19, 2019 at 19:33
  • 1
    Yes, usually you should use (event) => this.doSomething(event, additionalArguments). But if your doSomething doesn't care, you can of course omit it. Commented Sep 19, 2019 at 20:53
  • Thanks for the information @bergi , that makes sense Commented Sep 21, 2019 at 1:02

1 Answer 1

1

Refer to Scope:

// (a,b) are parameters passed by `Component` to `onClick` handler.
<Component onClick={(a,b) => this.doSomething(d)}/>

// Simple class example
class App extends React.Component {

  doSomething = (a,b) => {
    console.log('a',a,'b',b);
  }

  render() {
//                    onClick={this.doSomething}
    return <Component onClick={(a,b) => this.doSomething(a,b)}/>
  }
}

class Component extends React.Component {

  coolClick = () => {
    this.props.doSomething('hello','world')
  }

  render() {
    return <button onClick={this.coolClick}>Active doSomething</button>
  }
}

// d is a parameter available in the current scope
<Component onClick={() => this.doSomething(d)}

// Simple examples of possible scopes
import { d } from './consts'

const d = 5;

class App extends React.Component {

  doSomething = (x) => {
    console.log('x',x);
  }

  render() {
    const d = 5;
    return <Component onClick={() => this.doSomething(d)}/>
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this! But for more clarification, in this example: <ul> {this.state.letters.map(letter => <li key={letter} onClick={() => this.handleClick(letter)}> {letter} </li> )} </ul> Shouldn't you be able to pass in the "letter" parameter as an argument in the arrow function, and this.handleClick should know what it is because it was passed in? B/c my confusion is, the function inside the fat arrow should need to know what variable it is using by what was passed in from the actual arrow function parameter.
@BrianisWinston I don't get what your confusion is, which of the two arrow functions in your snippet you are talking about, and whether it is working as expected for you or not.
@Bergi I'm referring to the onClick function. Because there is a variable "letter" coming from the map function on the outside, it seems like you would have to pass in "letter" as a parameter in the onClick fat arrow function in order for this.handleClick(letter) to be able to use it. Although the documentation clearly says you don't need to pass in a parameter, I would just like clarification for myself.
@BrianisWinston No. As you said, it's coming from outside (through closure), it is not coming from a parameter of onClick. This means that onClick() can be called with no arguments (and it uses the letter from the map callback parameter) - if you had declared it as a parameter, then the caller of onClick(…) would need to provide the value, and that's not what we want here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.