reactjs - Get refs via function call -


i'm trying replicate react code this official guide:

  render: function() {     return <textinput ref={(c) => this._input = c} />;   },   componentdidmount: function() {     this._input.focus();   }, 

basically, i'm using es5 , tried this:

 componentdidmount: function() {         this.searchbox.focus();     },      render: function() {         return (             <input type="text" ref={function(c) {this.searchbox = c}} />         );     } 

but got error saying:

uncaught typeerror: cannot read property 'focus' of undefined 

isn't code above supposed same es6 version? don't why it's not working. in case you're interested in full code, here is: https://jsfiddle.net/xpd85ehx/

es5 , es6 arrow notations use different this.

specifically, using arrow notation, this lexically bound surrounding function (so refers class).

using es5 need bind this function.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -