【发布时间】:2017-03-07 18:58:13
【问题描述】:
我正在尝试将表单的状态映射到我的 LoginForm 组件的 props,并且状态显示为 undefined。
import React, { PropTypes } from 'react';
import { reduxForm, getFormValues } from 'redux-form/immutable';
import { connect } from 'react-redux';
import { logUserIn } from '../../actions/authentication';
import { VALID_EMAIL_REGEX } from '../../config/app_config';
import LoginForm from './LoginForm';
const FORM_ID = 'loginForm';
export class LoginFormContainer extends React.Component {
static propTypes = {
handleSubmit: PropTypes.func.isRequired,
submitting: PropTypes.bool.isRequired,
loginAction: PropTypes.func.isRequired,
values: PropTypes.object.isRequired,
};
performLogin = (params) => {
const { loginAction } = this.props;
const credentials = {
email: params.email,
password: params.password,
};
loginAction(credentials, '/home');
}
render() {
const { handleSubmit, submitting, values, ...others } = this.props;
console.log(values); // <--- undefined
return (
<LoginForm
handleSubmit={ handleSubmit }
loginFunction={ this.performLogin }
submitting={ submitting }
{...others}
/>
);
}
}
// there is a validate method here
LoginFormContainer = reduxForm({
form: FORM_ID, // <--- loginForm
validate,
})(LoginFormContainer);
const mapStateToProps = (state) => {
const values = getFormValues(FORM_ID)(state);
return {
values,
};
};
export default connect(mapStateToProps, {
loginAction: logUserIn,
})(LoginFormContainer);
这里是 LoginForm 组件:
import React, { PropTypes } from 'react';
import { Field, Form } from 'redux-form/immutable';
import { getClassName } from '../../utils/forms';
import { Link } from 'react-router';
// import { checkButtonDisabled } from '../../utils/forms';
const renderInput = ({ input, label, type, meta: { touched, error } }) => {
return (
<div className={ getClassName(touched, error) }>
<label className="form-control-label">
{ label }
{touched && error &&
<span className="error">{ error }</span>}
</label>
<input
{ ...input }
className="form-control form-control-success"
type={ type }
/>
</div>
);
};
export default class LoginForm extends React.Component {
static propTypes = {
handleSubmit: PropTypes.func.isRequired,
loginFunction: PropTypes.func.isRequired,
submitting: PropTypes.bool.isRequired,
};
render() {
const {
handleSubmit,
loginFunction,
submitting } = this.props;
return (
<Form id="loginForm" onSubmit={ handleSubmit(loginFunction.bind(this)) }>
<fieldset>
<Field
name="email"
component={renderInput}
type="text"
placeholder="example@exampledomain.com"
label="Email address"
/>
<Field
name="password"
component={renderInput}
type="password"
placeholder="your password"
label="Password"
/>
</fieldset>
<button
type="submit"
className="btn btn-primary"
disabled={ submitting }
>
Log In
</button>
<Link to="/forgot-password">Forgot Password?</Link>
</Form>
);
}
}
如果我只是将状态映射到 props 并在控制台记录它,它也会返回为 undefined。
我正在尝试获取值以在我的提交按钮上设置条件以禁用但我无法访问表单状态以运行任何 redux-form 方法来获取表单值,因为状态未定义。
这是该州的图片:
有人发现可能是什么问题吗?
【问题讨论】:
-
你确定你有
loginFormform吗? -
我附上一张图片来显示控制台中显示的状态。
-
看起来您在 loginAction 之前没有 loginForm,或者可能是另一个操作填充状态。尝试检查,
const values = form.loginForm && form.loginForm.values? form.loginForm.values : null;
标签: reactjs redux redux-form