我基于redux-form-material-ui repo的官方示例。我的代码如下所示:
import React from 'react';
import { Field } from 'redux-form';
import { TextField } from 'redux-form-material-ui';
export default class FormTextField extends React.Component {
constructor(props) {
super(props);
}
componentWillUpdate(nextProps) {
const { name, withRef, focus } = nextProps;
if (withRef && focus) {
this.refs[name]
.getRenderedComponent()
.getRenderedComponent()
.focus();
}
}
render() {
const { name, label, errorText, withRef } = this.props;
return (
<Field
name={name}
component={TextField}
hintText={label}
floatingLabelText={label}
errorText={errorText}
ref={name}
withRef={withRef}
/>
);
}
}
我只为第一个有错误的字段传递focus
和withRef
属性。在这个领域我总是得到一个错误:Uncaught (in promise) Error: If you want to access getRenderedComponent(), you must specify a withRef prop to Field(…)
我能够看到两者ref
并withRef
传递给Field
. 然后在组件中ref="wrappedInstance"
我仍然可以看到withRef="true"
. 它没有被更深地传递。
我将不胜感激任何想法如何解决它。