我试图将我的窗体的状态映射到我的LoginForm组件的道具,状态显示为undefined
。Redux状态在将状态映射到反应中的道具时未定义
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="[email protected]"
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>
);
}
}
如果我仅仅只映射状态道具和控制台日志,它也回来为undefined
。
我想获取值来设置我的提交按钮被禁用的条件,但我无法访问表单状态来运行任何的redux-form方法来获取表单值,因为状态是未定义的。
这里是国家的一个画面:
任何人发现可能是什么问题呢?
你确定你在'form'有'loginForm'吗? – Andrew
我附上了一张图片来显示控制台中显示的状态。 –
看起来你在loginAction之前没有loginForm,或者是maby其他操作填充状态。 尝试检查,用'const values = form.loginForm && form.loginForm.values? form.loginForm.values:null; ' – Andrew