最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React onClick handler in map function - Stack Overflow

programmeradmin1浏览0评论

I've been searching the web for answers to my question, but without success. I am trying to add a simple react click handler to my button, but I can't seem to make it work. It is probably something really simple, I just can't wrap my head around it.

Here is my code:

 export default class ReviewBox extends Component {

 deleteReview() {
  console.log("hey");
 }

 render() {
  const {reviews, date, lectureId} = this.props;
  const that = this;
    return (
      <div className="container content-sm">

        <div className="headline"><h2>Reviews</h2> <span>{date}</span></div>
          <div className="row margin-bottom-20">

            {reviews.map(review => {
                return(
                  <div className="col-md-3 col-sm-6">
                    <div className="thumbnails thumbnail-style thumbnail-kenburn">
                      <div className="caption">
                        <h3>{reviewment}</h3> <br />
                        <button className="btn btn-danger" onClick={this.deleteReview()}>Delete</button>
                      </div>
                    </div>
                  </div>
                )
            })}

          </div>

          <hr />
          <AddReview lectureId={lectureId} />

      </div>
    )
  }
}

It refuses to fire the function when I click a button. I've tried with .bind(this) and onClick={() => this.deleteReview} etc.

All help appreciated!

I've been searching the web for answers to my question, but without success. I am trying to add a simple react click handler to my button, but I can't seem to make it work. It is probably something really simple, I just can't wrap my head around it.

Here is my code:

 export default class ReviewBox extends Component {

 deleteReview() {
  console.log("hey");
 }

 render() {
  const {reviews, date, lectureId} = this.props;
  const that = this;
    return (
      <div className="container content-sm">

        <div className="headline"><h2>Reviews</h2> <span>{date}</span></div>
          <div className="row margin-bottom-20">

            {reviews.map(review => {
                return(
                  <div className="col-md-3 col-sm-6">
                    <div className="thumbnails thumbnail-style thumbnail-kenburn">
                      <div className="caption">
                        <h3>{review.ment}</h3> <br />
                        <button className="btn btn-danger" onClick={this.deleteReview()}>Delete</button>
                      </div>
                    </div>
                  </div>
                )
            })}

          </div>

          <hr />
          <AddReview lectureId={lectureId} />

      </div>
    )
  }
}

It refuses to fire the function when I click a button. I've tried with .bind(this) and onClick={() => this.deleteReview} etc.

All help appreciated!

Share Improve this question asked Nov 22, 2016 at 10:49 Mathias LundMathias Lund 7722 gold badges9 silver badges24 bronze badges 1
  • 3 You should pass to onClick reference to function onClick={ this.deleteReview } - remove () after function name – Oleksandr T. Commented Nov 22, 2016 at 10:50
Add a ment  | 

4 Answers 4

Reset to default 7

I think you are missing braces () in arrow function

<button className="btn btn-danger" onClick={() => this.deleteReview()}>Delete</button>

i think this will help you.....

export default class ReviewBox extends Component {

 deleteReview() {
  console.log("hey");
 },

 render() {
  const {reviews, date, lectureId} = this.props;
  const that = this;
    return (
      <div className="container content-sm">

        <div className="headline"><h2>Reviews</h2> <span>{date}</span></div>
          <div className="row margin-bottom-20">

            {reviews.map(review => {
                return(
                  <div className="col-md-3 col-sm-6">
                    <div className="thumbnails thumbnail-style thumbnail-kenburn">
                      <div className="caption">
                        <h3>{review.ment}</h3> <br />
                        <button className="btn btn-danger" onClick={this.deleteReview}>Delete</button>
                      </div>
                    </div>
                  </div>
                )
            })}

          </div>

          <hr />
          <AddReview lectureId={lectureId} />

      </div>
    )
  }
}

Removing () from the onClick function call and using { this.deleteReview } will indeed fire up the method, but if you need to bind this as well inside that method, go with @duwalanise answer.

Ah, now I understand. It is because I am rendering the react on serverside, that's why the click handler doesn't work.

I will have to render the JS on the client, in order for it to work :)

发布评论

评论列表(0)

  1. 暂无评论