react: Refs - "object is not extensible"
I wanted to drop my string-based refs bindings and replace them with callback-based ones, however, when I tried to replace ref="email" with ref={ (ref) => this.refs.email = ref }, I received an error: TypeError: Can't add property email, object is not extensible.
I didn’t find any informations about this in docs, but I found other issue in which @frederickfogerty did what I tried to do: ref={ c => this.refs['wrapper'] = c }
… and I assume that back in 22 Sep 2015 it worked, now it doesn’t. Did something change and refs object became nonextensible? Is assigning refs manually through callbacks to this.refs considered a bad practice now?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 6
- Comments: 15 (1 by maintainers)
@vishwa3 you need to use
this.query.current.value, notthis.query.valueYou can unfreeze this object by reassigning it in
componentWillMount:@biphobe
this.refsis a frozen object. Assign your references tothisdirectly.@satya164 :Thanks
@gaearon
Here is example that throws very same error Docs https://reactjs.org/docs/uncontrolled-components.html
And I get this:
Environment:
Please provide an example reproducing this.
AFAIK this was never good practice or recommended. Just store it directly on the instance (
this).This answer should be pinned above 👍