简体   繁体   English

使用Apollo和GraphQL的React表单不会触发onSubmit

[英]React form using Apollo & GraphQL does not trigger onSubmit

I can't seem to get onSubmit to trigger. 我似乎无法触发onSubmit Can anyone spot what's the issue here? 谁能发现这里的问题? I'm using Ant Design, but I don't think that's the issue. 我正在使用Ant Design,但我不认为这是问题。

Note: Currently, the GraphQL mutation below (via Apollo) does get sent to the back-end properly. 注意:目前,下面的GraphQL突变(通过Apollo)确实已正确发送到了后端。 Removing onClick in the Button does trigger onSubmit , but the GraphQL query does not get sent. Button中删除onClick会触发onSubmit ,但不会发送GraphQL查询。

Update: I haven't found a solution but I found that if I remove this line if (loading) return <p>loading...</p> , then onSubmit gets triggered. 更新:我还没有找到解决方案,但是我发现,如果删除了if (loading) return <p>loading...</p>这一行,则会触发onSubmit

 import React from 'react' import gql from 'graphql-tag' import { Mutation } from 'react-apollo' import { Form, Input, Button, message } from 'antd' import { __log } from '../utils/log.js' const { TextArea } = Input const ADD_MENTAL_MODEL_MUTATION = gql` mutation AddMentalModel($mentalModelText: String!, $thoughts: [String!]){ addMentalModel(text: $mentalModelText, thoughts: $thoughts) { text id } } ` class MentalModelForm extends React.Component { // constructor(props){ // super(props) // this.handleOnSubmit = this.handleOnSubmit.bind(this) // } handleOnSubmit = e => { console.log('handleOnSubmit IS EXECUTED!!!!!') e.preventDefault() debugger } _hasErrors = (fieldsError) => { return Object.keys(fieldsError).some(field => fieldsError[field]) } render(){ const { getFieldDecorator, isFieldTouched, getFieldError, getFieldValue, getFieldsError } = this.props.form return( <Form onSubmit={ e => this.handleOnSubmit(e)}> {/* <Form onSubmit={this.handleOnSubmit}> */} <Form.Item label='Mental Model'> { getFieldDecorator( 'mentalModel', { rules: [{required: false, whitespace: false, message: 'Missing or too short of a mental model'}] } )(<TextArea />) } </Form.Item> <Form.Item label='Thought'> { getFieldDecorator( 'thought', { rules: [{required: false, whitespace: false, message: 'Must add a thought'}] } )(<Input />) } </Form.Item> <Form.Item> <Mutation mutation={ADD_MENTAL_MODEL_MUTATION} variables={ { mentalModelText: getFieldValue('mentalModel'), thoughts: [getFieldValue('thought')], } } > { (mutation, {loading, error, data}) => { if (loading) return <p>loading...</p> return( <Button type='primary' htmlType='submit' onClick={mutation} disabled={this._hasErrors(getFieldsError())} > Add new mental model </Button> ) } } </Mutation> </Form.Item> </Form> ) } // render } // class export default Form.create({name: 'mentalmodel_hoc'})(MentalModelForm) 

I think that the Button type should be submit instead of primary 我认为应该将Button类型而不是主要类型提交

<Button
   type='submit'
   onClick={mutation}
   disabled={this._hasErrors(getFieldsError())}
   >
   Add new mental model
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM