(Redux表单)您必须将handleSubmit()传递给onSubmit函数或传递onSubmit作为prop

时间:2017-10-30 14:25:22

标签: javascript reactjs redux redux-form

我是一名超级React-redux初学者,刚开始研究我的第一个个人项目。我尝试使用Redex-form构建提交表单,我得到了第一个错误,我从来没有得到过...这是我唯一可以提出问题的地方。

如果您想查看整个项目文件,请参阅我的github https://github.com/jlee380/cal-frontend

这是 submit_form.js

import React, { Component } from 'react';
import { reduxForm } from 'redux-form';
import { submitForm } from '../actions/action_submit_form';
// import { Link } from 'react-router';


class SubmitForms extends Component {
   onSubmit(props) {
     this.props.submitForm(props)
       .then(() => {
          console.log("good submit");
       });
   }

  render() {
    const { fields: { gender, age, height, weight, activity }, 
    handleSubmit } = this.props;

return (
    <form onSubmit={handleSubmit(this.props.submitForm)}>
    <div className="form-group">
      <label className="control-label">Gender</label>
      <select type="gender" className="form-control" id="gender_id" name="gender_name" { ...gender }>
        <option>Male</option>
        <option>Female</option>
      </select>
    </div>
    <div className="form-group">
      <label className="control-label">Age</label>
      <input type="age" className="form-control" id="age_id" name="age_name" { ...age } />
    </div>
    <div className="form-group">
      <label className="control-label">Height</label>
      <input type="height" className="form-control" id="height_id" name="height_name" { ...height } />
    </div>
    <div className="form-group">
      <label className="control-label">Weight</label>
      <input type="weight" className="form-control" id="weight_id" name="weight_name" { ...weight } />
    </div>
    <div className="form-group">
      <label className="control-label">Activity</label>
      <select type="activity" className="form-control" id="activity_id" name="activity_name" { ...activity }>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select>
    </div>

    <div className="form-group">
      <button type="submit" className="btn btn-primary">Submit</button>
    </div>
  </form>
);
}
}

export default reduxForm({
  form: 'SubmitNewForm',
  fields: ['gender', 'age', 'height', 'weight', 'activity']
}, null, { submitForm })(SubmitForms);

这是动作创建者 action_submit_form.js

import axios from 'axios';

export const ADD_POST = 'ADD_POST';
const BASE_URL = 'http://138.197.166.14:8000';


export function submitForm(props) {
  console.log('action is hit:');
  const calculatedResult = axios.get(`${BASE_URL}/getcalories`);

  return {
    type: ADD_POST,
    payload: calculatedResult
  };
}

这是reducer index.js reducer_submit_form.js

import reducerSubmitForm from './reducer_submit_form';
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers ({
  post: reducerSubmitForm,
  form: formReducer
});

export default rootReducer;

import { ADD_POST } from '../actions/action_submit_form';

const INITIAL_STATE = { all: [], post: null };

export default function(state = INITIAL_STATE, action) {
  switch (action.type) {
    case 'ADD_POST':
      return{ ...state, all: action.payload.data };
    default:
      return state;
  }

}

2 个答案:

答案 0 :(得分:1)

我认为错误非常简单。

检查SubmitForms类呈现方法中的以下行:

<form onSubmit={handleSubmit(this.props.submitForm)}>

如果你记录这个,你的输出是什么?

handleSubmit(this.props.submitForm)

根据redux格式documentation,必须将提交函数传递给onSubmit。

答案 1 :(得分:0)

onSubmit函数应在reduxForm的调用中设置。您<form>标记onSubmit处理程序应该只是对Redux Form为您注入的函数handleSubmit的引用。我同意命名有点混乱。 :)

请参阅文档中的示例: https://redux-form.com/7.1.2/docs/gettingstarted.md/#step-2-of-4-form-component

let ContactForm = props => {
  const { handleSubmit } = props
  return (
    <form onSubmit={ handleSubmit }>
      { /* form body*/ }
    </form>
  )
}

ContactForm是使用reduxForm装饰的组件,如下所示:

ContactForm = reduxForm({
  // a unique name for the form
  form: 'contact',
  // you can define your custom onSubmit here!
  onSubmit: ...
})(ContactForm)