无法读取未定义的属性“提交”-React

时间:2019-01-04 11:09:33

标签: reactjs formwizard react-final-form

我用react-final-form创建向导表单。当我单击按钮时,出现此错误: enter image description here

我不明白为什么会出现这样的消息。这是我的向导组件。

import { Form as FinalForm } from 'react-final-form'; 
import { Button, Form, } from 'reactstrap';
    class Wizard extends Component {
      static Page = ({ children }) => children

      constructor(props) {
        super(props);
        this.state = {
          page: 0,
          values: props.initialValues || {},
        };
      }

      next = (values) => {
        const { children } = this.props;
        this.setState((state) => ({
          page: Math.min(state.page + 1, React.Children.toArray(children).length - 1),
          values,
        }));
        console.log('ha');
      }

      previous = () => {
        this.setState((state) => ({
          page: Math.max(state.page - 1, 0),
        }));
      }

      validate = (values) => {
        const { children } = this.props;
        const { page } = this.state;
        const activePage = React.Children.toArray(children)[
          page
        ];
        return activePage.props.validate ? activePage.props.validate(values) : {};
      }

      handleSubmit = (values) => {
        const { children, onSubmit } = this.props;
        const { page } = this.state;
        const isLastPage = page === React.Children.count(children) - 1;
        if (isLastPage) {
          onSubmit(values);
        } else {
          this.next(values);
        }
        console.log(children);
        console.log(React.Children.toArray(children));
        console.log('hao');
      }

      render() {
        const { children } = this.props;
        const { page, values } = this.state;
        const activePage = React.Children.toArray(children)[page];
        const isLastPage = page === React.Children.count(children) - 1;
        return (
          <FinalForm
            initialValues={values}
            validate={this.validate}
            onSubmit={this.handleSubmit}
          >
            {
              ({
                handleSubmit,
                submitting,
                pristine,
                invalid,
              }) => (
                <Form onSubmit={handleSubmit}>
                  {activePage}
                  <div className="buttons">
                    {page > 0 && (
                      <Button type="button" onClick={this.previous}>
                        « Previous
                      </Button>
                    )}
                    {!isLastPage && <Button color="success" type="submit">Next »</Button>}
                    {isLastPage && (
                      <Button color="success" type="submit" disabled={submitting || pristine || invalid}>
                        Submit
                      </Button>
                    )}
                  </div>
                </Form>
              )
            }
          </FinalForm>
        );
      }
    }

当我单击按钮时,可能未触发功能handleSubmit,因为未显示console.log。 谁能知道为什么会出现这样的消息?感谢您的提前帮助。

1 个答案:

答案 0 :(得分:1)

您的按钮没有onClick事件:

        {!isLastPage && <Button color="success" type="submit">Next »</Button>}
                {isLastPage && (
                  <Button color="success" type="submit" disabled={submitting || pristine || invalid}>
                    Submit
                  </Button>
                )}

所以他们应该像这样:

        {!isLastPage && <Button color="success" type="submit" onClick={handleSubmit}>Next »</Button>}
                {isLastPage && (
                  <Button color="success" type="submit" disabled={submitting || pristine || invalid} onClick={handleSubmit}>
                    Submit
                  </Button>
                )}