javascript - this.refs.something returns "undefined" -
i have element ref
defined , ends getting rendered page :
<div ref="russian" ...> ... </div>
i want access dom element properties offset...
or something. however, keep getting undefined
, haven't faintest idea why. after searching it's clear refs
applicable 1 file i'm not using anywhere besides 1 page. i'm saying log it:
console.log('refs', this.refs.russian);
what causing this?
the correct place work refs
inside specific react lifecycle methods e.g. componentdidmount, componentdidupdate
you cannot reference refs
render()
method. read more cautions of working refs
here.
if move console.log('refs', this.refs.russian);
call componentdidmount
or componentdidupdate
lifecycle methods (assuming on react >= 14) should not undefined result.
update: also refs not work on stateless components per caution link above
Comments
Post a Comment