0

Hi so I have this fiddle here http://jsfiddle.net/cmrunhow/5/ and it is working fine. However I was wondering why can't I replace

var clickHandler = this.props.onRatingSelected && this.props.onRatingSelected.bind(null, i);
items.push(<li key={i} className={i <= this.props.value && 'filled'} onClick={clickHandler}>{'\u2605'}</li>);

with

items.push(<li key={i} className={i <= this.props.value && 'filled'} onClick={this.props.onRatingSelected.bind(null, i)}>{'\u2605'}</li>);

assuming I skip the sanity check.

2 Answers 2

3

in your fiddle FundooRating component is called with and without onRatingSelected props.

 <div>
  Rating is {this.state.rating} <br/>
  Clickable Rating <br/>
  <FundooRating value={this.state.rating} max="10" onRatingSelected={this.handleRatingSelected.bind(this)} />
  <br />
  Readonly rating <br/>
  <FundooRating value={this.state.rating} max="10" />
</div>

so you will have to check for null case in replaced code too

items.push(<li key={i} className={i <= this.props.value && 'filled'} onClick={this.props.onRatingSelected && this.props.onRatingSelected.bind(null, i)}>{'\u2605'}</li>);

Sample Fiddle

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

4 Comments

Just a pointer, instead of .bind(null, i) do .bind(this, i)
Ahhh yes, I was too obsessed with the first row of stars I totally neglected the second row. A quick follow up question tho, using the same fiddle with some changes here jsfiddle.net/cmrunhow/7 , I realized React with ES6 no longer autobinds method and one of the alternative is to use fat arrow function. Could you take a look and tell me where have I gone wrong?
I believe fat arrow functions are not supported in ES6 classes.
To my knowledge it should have worked. There is another way of using fat arrow. jsfiddle.net/y944zuoc/1
0

You need the check for

this.props.onRatingSelected

because you have an instance of FundooRating your where you don't pass a handler as a prop:

 <FundooRating value={this.state.rating} max="10" />

(just below the Readonly text).

Comments

Your Answer

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