【问题标题】:Call a GraphQL Mutation once after React component mounts在 React 组件挂载后调用一次 GraphQL Mutation
【发布时间】:2020-06-18 18:06:03
【问题描述】:

用户创建个人资料后,他们会在电子邮件中收到一个链接,该链接会将他们发送回网站,网址中包含 verifyToken。如果令牌与存储在数据库中的令牌匹配,则它们的isVerified 状态将存储在数据库中,值为true

new-profile.js

import VerifyEMail from '../components/VerifyEmail';

const NewProfilePage = props => (
  <div>
    <VerifyEMail verifyToken={props.query.verifyToken} />
  </div>
);

export default NewProfilePage;

目前,我已经实现并使用带有“验证”按钮的表单工作,用户必须单击该按钮才能调用 graphQL 突变,verifyEmail。由于这会将数据库中的 isVerified 值设置为 true,因此我知道一切正常。

../components/VerifyEmail.js

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Mutation } from 'react-apollo';
import gql from 'graphql-tag';

const VERIFY_EMAIL_MUTATION = gql`
  mutation VERIFY_EMAIL_MUTATION($verifyToken: String!) {
    verifyEmail(verifyToken: $verifyToken) {
      isVerified
    }
  }
`;

class VerifyEmail extends Component {
  render() {
    const { verifyToken } = this.props;
    return (
      <Mutation mutation={VERIFY_EMAIL_MUTATION} variables={{ verifyToken }}>
        {verifyEmail => (
          <form
            onSubmit={async () => {
              await verifyEmail(verifyToken);
            }}
          >
            <button type="submit">Verify</button>
          </form>
        )}
      </Mutation>
    );
  }
}

VerifyEmail.propTypes = {
  verifyToken: PropTypes.string.isRequired,
};

export default VerifyEmail;

但是,我真的不想强迫我的用户必须单击按钮才能触发突变。我希望在组件加载后调用它。我已经为此绞尽脑汁了一天半,尝试了很多东西,但似乎找不到任何有效的方法。

我已经看到了一些使用React hooksApollo hooks、componentDidMount 等的解决方案。我的大脑只是很难再看到它。这个链接有一些迄今为止我发现的最好的解决方案,但我不知道如何实现它们...... [Feature idea] Execute a mutation on mount #1939

非常感谢任何能帮助我指明正确方向的人。谢谢。

【问题讨论】:

    标签: reactjs graphql apollo react-apollo


    【解决方案1】:

    使用 React 钩子时,这是一个更简单的应用程序:

    import React, { useEffect } from "react";
    
    function VerifyEmail({ verifyToken }) {
      const [ verifyEmail, { loading, data, error }] = useMutation(VERIFY_EMAIL_MUTATION);
      useEffect(() => {
        verifyEmail({
          variables: { verifyToken },
        });
      }, []);
      return (
        <>
          {loading && <p>Loading...</p>}
          {data && <p>Verified successfully!</p>}
          {error && <p>Error!</p>}
        </>
      )
    }
    

    如果您想继续使用类,唯一的解决方案是创建一个组件并为此目的使用该组件的componentDidMount

    // Current component:
    <Mutation mutation={VERIFY_EMAIL_MUTATION} variables={{ verifyToken }}>
      {verifyEmail => (
        <SendEmail token={verifyToken} verify={verifyEmail} />
      )}
    </Mutation>
    
    // Send Email component
    class SendEmail extends Component {
      componentDidMount() {
        const { token, verify } = this.props;
        verify(token);
      }
      render() {
        return (
          //handle loading, data and error states
        )
      }
    }
    

    【讨论】:

    • 这成功了!谢谢你。我曾尝试在功能组件中使用 useEffect() 钩子几次,但现在我发现我用错了。我想我需要更多地研究 React Hooks。如果这有助于其他人,您可能想要添加到您的答案中的一件事是从“react-apollo”导入 { useMutation }。那是我采取的另一条路线,但不太确定如何实施。我想最后,我试图让它变得比它需要的更复杂。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2019-06-30
    • 2022-06-12
    • 2019-11-23
    • 2020-12-01
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多