无法从表单获取值-Redux表单

时间:2018-08-15 21:07:41

标签: reactjs redux redux-form

我正在尝试进行remoteSubmit https://redux-form.com/7.4.2/examples/remotesubmit/ 使用Redux Forms,但我无法从实际表单中获取值,而不能真正确定发生了什么。这是我的代码:

表单容器:

class LoginFormContainer extends Component {

handleSubmit = () => {
  return this.props.dispatch(submit('loginForm'));
};

render() {

  const {open, close} = this.props;

  return(
    <Dialog
      open={open}
      onClose={close}>
      <DialogTitle>Sign In</DialogTitle>
      <DialogContent>
        <DialogContentText>
          Please sign in to your account using your username and password.
        </DialogContentText>
        <LoginForm/>
      </DialogContent>
      <DialogActions>
        <Button onClick={close} color='primary'>Cancel</Button>
        <Button onClick={this.handleSubmit} color='primary'>Sign in</Button>
      </DialogActions>
    </Dialog>
  );
  }

}

export default connect()(LoginFormContainer);

表格:

class LoginForm extends Component {

  render() {
    const {handleSubmit} = this.props;

    return (
      <form onSubmit={handleSubmit}>
        <Field
          component={TextField}
          name='username'
          type='text'
          label='Username'
          autoFocus
          margin='dense'
          fullWidth/>
        <Field
          name='password'
          component={TextField}
          type='password'
          label='Password'
          margin='dense'
          fullWidth/>
      </form>
    );
  }
}

LoginForm = reduxForm({
  form: 'loginForm',
  onSubmit: login
})(LoginForm);

export default LoginForm;

这是我要触发的操作:

export const login = (values) => {
  console.log('!!!!!!', values);
  return dispatch => {
    fetch(endpoints.auth.login, {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(values)
    })
      .then(res => res.json())
      .then(data => dispatch(setUser(data)))
  }
};

我在console.log上找到:!!!!!!,{}

关于我在做什么错的任何想法?

0 个答案:

没有答案