[英]how to access reducer in validate.js for redux-form
所以我想從我的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);
如何訪問狀態,以便可以從我的減速器獲取數據? 我嘗試研究文檔以及其他所有內容,但似乎找不到找到狀態的方法。
我嘗試使用this.state
但它返回的值未定義。
您需要檢查這些values
,就像這樣-
const validate = values => {
const errors = {};
if (!values.numberOnCard) {
errors.numberOnCard = 'Required';
}
return errors;
};
我建議瀏覽一下文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.