0

我有一个反应原生组件。我得到了错误:

Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.

代码:

import....

class Register extends Component {
  static navigationOptions = {
    header: null,
  };

  async handleSubmit(values, customerCreate) {
    const { email, password, firstName, lastName, phone } = values;
    const input = { email, password, firstName, lastName, phone };
    const customerCreateRes = await customerCreate({ variables: { input } });
    const isCustomerCreated = !!customerCreateRes.data.customerCreate.customer.id;
    if (isCustomerCreated) {
      const isStoredCrediential = await storeCredential(email, password);
      if (isStoredCrediential === true) {
        // Store in redux
        // Go to another screen
        console.log('test');
      }
    }
  }

  render() {
    return (
      <Mutation mutation={CREATE_CUSTOMER_ACCOUNT}>
        {
            (customerCreate, { error, data }) => {

              return (
                <MainLayout
                  title="Create Account"
                  backButton
                  currentTab="profile"
                  navigation={this.props.navigation}
                >
                  { showError }
                  { showSuccess }
                  <RegistrationForm
                    onSubmit={async (values) => this.handleSubmit(values, customerCreate)}
                    initialValues={this.props.initialValues}
                  />
                </MainLayout>
              );
            }
          }
      </Mutation>

    );
  }
}

const mapStateToProps = (state) => {
  return {
    ....
  };
};

export default connect(mapStateToProps)(Register);

CREATE_CUSTOMER_ACCOUNT 是 graphql:

import gql from 'graphql-tag';

export const CREATE_CUSTOMER_ACCOUNT = gql`
mutation customerCreate($input: CustomerCreateInput!) {
  customerCreate(input: $input) {
    userErrors {
      field
      message
    }
    customer {
      id
    }
  }
}
`;

更多细节在这里

谁在使用handleSubmit?

当按下时,表单中有一个按钮调用handleSubmit。

这个语法正确 onPress={handleSubmit} 吗?

const PrimaryButton = ({ label, handleSubmit, disabled }) => {
  let buttonStyle = styles.button;
  if (!disabled) {
    buttonStyle = { ...buttonStyle, ...styles.primaryButton };
  }

  return (
    <Button block primary={!disabled} disabled={disabled} onPress={handleSubmit} style={buttonStyle}>
      <Text style={styles.buttonText}>{label}</Text>
    </Button>
  );
};

导出默认 PrimaryButton;

更新 1: 如果我删除customerCreate(来自 graphql),错误就会消失。这意味着异步等待实际上是正确的,但我需要 customerCreate

4

3 回答 3

0

您是否检查过以下代码?

onSubmit={(values) => this.handleSubmit(values, customerCreate)}
于 2019-03-04T04:05:31.623 回答
0

事实证明异步等待语法是正确的。完整的原始代码(此处未发布)包含Toast component react-base. 其他开发人员能够告诉我删除它并且错误消失了。有时很难调试。

于 2019-03-05T02:51:16.537 回答
0

如果您尝试在 recompose 中向处理程序添加参数,请确保您在处理程序中正确定义了参数。也可能是您不小心在渲染方法中调用了 onSubmit 方法,您可能需要仔细检查您onSubmit在 RegistrationForm 组件中的方式。此外,您可能还想尝试另一件事,async handleSubmit(values, customerCreate) {移至handleSubmit = async(values, customerCreate) =>; 如果这不起作用,请也添加您的RegistrationForm组件。

底线,除非您没有在渲染中设置状态,否则这不会发生。

于 2019-03-04T06:04:28.510 回答