所以我想从我的reducer访问一些数据,所以我可以在我的validate.js中使用它来进行redux-form验证
const validate = (values, props) => {
const errors = {};
state.get('deposits').depositMethod // this is what I want to achieve
return errors;
};
class VisaDeposit extends React.Component {
render() {
const {
/* eslint-disable react/prop-types */
handleSubmit,
pristine,
reset,
submitting,
selectedCard,
} = this.props;
return (
<form onSubmit={handleSubmit}>
<div className={s.newField}>
<div>
<Field
id="numberOnCard"
label="Card number"
name="numberOnCard"
component={renderField}
type="text"
placeholder="Card number"
/>
</div>
</div>
<button
type="submit"
disabled={pristine || submitting}
className={s.submit}
>Submit
</button>
</form>
);
}
}
const selector = formValueSelector('accountDeposits');
const Form = reduxForm({
form: 'accountDeposits',
destroyOnUnmount: false,
forceUnregisterOnUnmount: true,
validate,
})(VisaDeposit);
const StyledForm = withStyles(s)(Form);
function mapStateToProps(state) {
return {
selectedCard: selector(state, 'selectedCard'),
};
}
function mapDispatchToProps(dispatch) {
return {
loadRegisteredCards: bindActionCreators(loadRegisteredCards, dispatch),
};
}
export default connect(mapStateToProps, mapDispatchToProps)(StyledForm);
如何访问状态以便从reducer中获取数据?我尝试研究文档和其他所有内容,但我似乎无法找到访问状态的方法。
我尝试使用this.state
但它返回的值是undefined。
答案 0 :(得分:0)
您需要检查values
,就像这样 -
const validate = values => {
const errors = {};
if (!values.numberOnCard) {
errors.numberOnCard = 'Required';
}
return errors;
};
我建议您浏览docs