React stateless component this.refs..value?

后端 未结 7 1723
故里飘歌
故里飘歌 2020-12-03 01:09

I don\'t know if I\'m doing this correctly... If I want to get value from an input I use this.refs.whatever.value.trim() but if that input is a stateless function component

7条回答
  •  长情又很酷
    2020-12-03 01:28

    Edit: Looks like this is not the issue anymore, since new ideas on how to handle this situation arose since this answer was written. Refer to inanc's answer instead of this one.

    Refs are unavailable in stateless components. From React Docs:

    Because stateless functions don't have a backing instance, you can't attach a ref to a stateless function component. Normally this isn't an issue, since stateless functions do not provide an imperative API. Without an imperative API, there isn't much you could do with an instance anyway. However, if a user wants to find the DOM node of a stateless function component, they must wrap the component in a stateful component (eg. ES6 class component) and attach the ref to the stateful wrapper component.

提交回复
热议问题