score:3

Accepted answer

the method setstate() is asynchronous, and updates are often batched together. in your case, ischecked is updated together with the weight, so when you set the weight you still refer to the old value.

one solution is to use setstate()'s callback that will be called after the state is updated.

note: to get the checkbox checked state, use the event object e passed to the handler instead of querying the dom.

handlecheckboxclick(e){
   var checked = e.target.checked;
   this.setstate({ischecked : checked}, function() {
     if(this.state.ischecked){
        this.setstate({weight:'bold'});
     }else{
        this.setstate({weight:'normal'});
     }     
   });
}

a better solution is to update both properties because you know if the checkbox is checked:

handlecheckboxclick(e){
   var checked = e.target.checked;

   this.setstate({
    ischecked : checked,
    weight: checked ? 'bold' : 'normal'
   });
}

score:0

thats cause this.setstate({ ischecked : checkbox }); has not finished before you ask it in the if statement.


Related Query

More Query from same tag