0

Suppose we have a method inside a class like this

class Blog extends Component {


  postClicked = (id) => {
    this.setState({selectedPostId: id})
  }

    render () {
      const newPosts = this.state.posts.map(el => {
        return <Post key={el.id}
         title={el.title}
         author={el.author}
         onClick={this.postClicked(el.id)}/>
      })

    return
    //something
{post}
  }
 }
}

Now, What is the difference between calling the handler like this

onClick={this.postClicked(el.id)} and onClick={() => this.postClicked(el.id)}

Would appreciate if someone can tell me the difference in general

6 Answers 6

2

after Ecmascript 6 javascript was introduced with is arrow function link

here ()==>{//code} is a similar as a function() or anonymous function

tell me if you find out what you want

Sign up to request clarification or add additional context in comments.

Comments

1

The first option, "this.postClicked(el.id)", will actually call the method, "this.postClicked", with the "el.id" argument, each time the component renders (probably not what's intended).

The second option, "() => this.postClicked(el.id)", will only call the method, "this.postClicked", with the "el.id" argument, when "Post" is clicked.

Overall, if you can find a way to put the "el.id" argument into an "id" or "name" prop on the component

<Post id={el.id} />

then you can do:

<Post
  id={el.id}
  onClick={this.postClicked}
/>

this.postClicked = (event) => {
  const { id } = event.target;
  ...
}

This last option avoids the use of an unnamed function. If you use an unnamed function, it will cause unnecessary re-renders. React cannot tell that an unnamed function is the same when it's checking whether or not it should re-render, by considering if the props of a component have changed. It considers the unnamed functions to be a new prop each time it checks, causing an unnecessary re-render each time.

Overall, it won't break your app, but it slows down performance slightly if you do it enough. It comes up especially if you start using React Motion (you'll really notice a difference there). It's best to avoid unnamed functions if possible.

Comments

1

you can read this blog it wil clear the things https://medium.com/@machnicki/handle-events-in-react-with-arrow-functions-ede88184bbb

Comments

1

Differences are,

  1. First method is a wrong implementation and it wont give the intended result, where as second one will work.
  2. In the first method you are making a function call, in second one you are assigning a function's signature to onClick. It is like the combination of below two statements.

    var variableName = function(){//some content};  
    onClick={variableName}
    

Comments

1

It looks like you question has already been answered. Just a side note though: remember that when assigning your method with an arrow function

onClick={ () => this.method() }

a new anonymous function is created on every re-render. So if the method doesn't need any arguments, it's better to reference the method directly (without parentheses so it's not invoked).

onClick={ this.method }

Comments

0

The first will call the function every time render is done.

The second will do what you want - call it onClick.

Comments

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.