React Native,在现有状态转换期间无法更新

时间:2019-03-04 00:48:09

标签: reactjs react-native async-await graphql graphql-mutation

我有一个react native组件。我得到了错误:

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
    }
  }
}
`;

More detail here

谁在使用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

3 个答案:

答案 0 :(得分:0)

您是否使用以下代码检查?

onSubmit={(values) => this.handleSubmit(values, customerCreate)}

答案 1 :(得分:0)

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

底线,除非您未在渲染中设置状态,否则不会发生这种情况。

答案 2 :(得分:0)

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