-1

我正在尝试使用 graphql 和 react.js 进行一些突变,但我遇到了问题。确实,我收到了以下消息:

ESLint: React Hook "useMutation" is called in function "onSubmit" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter.(react-hooks/rules-of-hooks)

但是当我单击以验证表单时我需要进行突变,为此我需要函数“onSUbmit”

这是我的代码:

import React from "react";
import { Modal, Button } from "react-bootstrap";
import {useForm} from "react-hook-form";
import {gql, useMutation, useQuery} from '@apollo/client';
import {BrowserRouter, Link, Redirect} from "react-router-dom";

const Register = (props) => {
  const { register, handleSubmit, errors  } = useForm();
  const onSubmit = data => {
    let username = data.Username;
    const GET_ACTIVITY = gql`
    mutation Register($username: String!){
    register(username: $username){
    username
    } 
    }
    `
    const [addchannel, { datas} ] = useMutation(GET_ACTIVITY);
    }
  console.log(props);

    return (
      <Modal show={props.show} onHide={props.onClose} centered>
        <div className="login-form">
          <h3 className="h3 mb-3 font-weight-normal" style={{textAlign: "center"}}> Register</h3>
          <form className="form-signin" onSubmit={handleSubmit(onSubmit)} >
            <div className="form-group">
            <input
              type="text"
              id="inputUsername"
              className="form-control"
              placeholder="Username"
              required=""
              autoFocus=""
              name="Username"
              ref={register({ required: true})}
            />
            <button className="btn btn-outline-success btn-block" type="submit" >
              <i className="fas fa-sign-in-alt" /> Register
            </button>
            <hr />
            </div>
          </form>
        </div>
      </Modal>
    );
  }

export default Register;

请问你能帮帮我吗 ?

非常感谢 !

4

2 回答 2

0

不要在循环、条件或嵌套函数中调用 Hook。

你可以试试这个。

const Register = (props) => {
  const { register, handleSubmit, errors  } = useForm();
  const [addChannel, { datas} ] = useMutation(GET_ACTIVITY);
  const GET_ACTIVITY= gql`
    mutation Register($username: String!){
      register(username: $username){
        username
      } 
    }
    `
  const onSubmit = data => {
    addChannel({ variables: { username: data.Username } });;
  }
  ...
于 2021-02-18T09:59:59.783 回答
0

这条线const [addchannel, { datas} ] = useMutation(GET_ACTIVITY);实际上并没有调用突变。它只是为您提供了执行此操作的方法。然后,您必须在代码中的其他地方调用 addChannel。这就是为什么对 useMutation 的调用必须在 onSubmit 函数之外的原因。然后在您的 onSubmit 函数中调用addChannel(). 然后组件将重新渲染,您可以使用datas.

编辑:在我看来,尽管您可以将用户名变量直接传递给模板文字。您永远不必这样做!即便如此,你必须像这样传递它:

gql`
    mutation Register($username: String!){
    register(username: ${$username}){
    username
    } 
    }
    `

但同样,您永远不必像这样构建动态查询。

你必须这样称呼你的突变:

addChannel({ // this is the function returned by useMutation
  variables: {
    username // that's where you pass username !
  }
})

所以,没有必要在你的函数中包含 GET_ACTIVITY,更不用说调用 useMutation。

于 2021-02-18T09:44:10.753 回答