我试图在我的容器中使用getFormValues添加道具的表单值。但是,当我在派生组件中查找values属性时,我得到一个未定义。我错过了什么?
容器代码:
import {reduxForm, getFormValues} from 'redux-form';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as actions from '../actions/propertiesActions';
import Properties from '../components/common/Properties';
const mapStateToProps = (state, ownProps) => {
return {
form: ownProps.id,
values: getFormValues(ownProps.id)(state)
};
};
const mapDispatchToProps = (dispatch) => {
return {
actions: bindActionCreators(actions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(reduxForm()(Properties));
在Properties组件中,调用this.props.values
将返回undefined。
有什么问题吗?谢谢你,
props«values»是redux形式的保留键。您需要尝试使用其他键,如«formValues»。
@connect(
(state) => ({
formValues: getFormValues('form-name')(state) || {},
}),
{}
)
@reduxForm({
form: 'form-name'
})
export default class Form extends Component {
static propTypes = {
...
formValues: PropTypes.object.isRequired,
...
}
render() {
return (
);
}
}