【发布时间】:2017-07-16 11:57:57
【问题描述】:
我正在尝试使用 React refs 在 Redux-Form Field 挂载时对其进行聚焦。
当我在componentDidMount 中尝试this.refs.title.getRenderedComponent().focus() 时,会抛出一个错误:
edit_fund.js:77 Uncaught TypeError: Cannot read property 'getRenderedComponent' of undefined
当我 console.log this.refs 时,它主要是一个空对象,有时将 'title' 标识为 ref,但它并不可靠。
我是否错误地使用了 refs?我的代码如下供参考。
componentDidMount = () => {
this.refs.title
.getRenderedComponent()
.focus();
}
...
<Field
id="title"
name="title"
component={FormInput}
type="text"
ref="title" withRef
/>
【问题讨论】:
-
这很奇怪,你的代码看起来很好,请显示整个组件代码
-
你解决了吗?
-
不,很遗憾从未解决过。
-
我得到了类似于这个工作的东西如果组件类型是一个简单的类型,比如
input。用户定义的组件会产生 refs,但它们不是支持像focus()这样的调用的 DOM 元素。相反,它们是 React 元素。
标签: javascript reactjs redux-form react-redux-form