我正在尝试使用 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;
请问你能帮帮我吗 ?
非常感谢 !