javascript - ReactJS Uncaught TypeError: Cannot read property '' of null when one method is applied in another -
i use in method visibilityfilter(event){...} method helperfunction(){...}. when push button fire event error:
uncaught typeerror: cannot read property 'helperfunction' of null
how can rid of error in following code:
class buttons extends react.component { helperfunction(){ let number = 2; return number }; visibilityfilter(event){ console.info(' helperfunction():',this.helperfunction()); //error here let action = {type: 'set_visibility_filter',payload: {name: event.target.dataset.filtername, on: true}}; store.dispatch(action); }; render(){ return( <div classname="container-fluid"> <div classname="row"> <div classname="col-lg-4 col-md-4 col-sm-4 col-xs-0"> <buttom type="button" classname="m-t-1-em w-100 btn btn-info" onclick={this.visibilityfilter} data-filter-name="favorite">favorite</buttom> </div> </div> </div> ); }; }; if(document.queryselector("#test1")){ reactdom.render( <buttons />, document.queryselector("#test1") ); };
with of dimitar christoff: solution problem lies in issue of binding. need add .bind(this) in following piece of code in constructor(props){...}:
this.visibilityfilter.bind(this);
the full code be:
class buttons extends react.component { constructor(props){ super(props); this.visibilityfilter.bind(this); }; helperfunction(){ let number = 2; return number }; visibilityfilter(event){ console.info(' helperfunction():',this.helperfunction()); let action = {type: 'set_visibility_filter',payload: {name: event.target.dataset.filtername, on: true}}; store.dispatch(action); }; render(){ return( <div classname="container-fluid"> <div classname="row"> <div classname="col-lg-4 col-md-4 col-sm-4 col-xs-0"> <buttom type="button" classname="m-t-1-em w-100 btn btn-info" onclick={this.visibilityfilter} data-filter-name="favorite">favorite</buttom> </div> </div> </div> ); }; }; if(document.queryselector("#test1")){ reactdom.render( <buttons />, document.queryselector("#test1") ); };
Comments
Post a Comment