【发布时间】:2021-11-29 07:15:40
【问题描述】:
我正在使用 react 和 antd 进行开发。
这是useAxios我写的。
import { useState, useEffect } from 'react';
import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:3000/';
const useAxios = ({ url, method, body = null, headers = null }) => {
const [response, setResponse] = useState(null);
const [error, setError] = useState('');
const [loading, setloading] = useState(true);
const fetchData = () => {
axios[method](url, headers, body)
.then((res) => {
setResponse(res.data);
})
.catch((err) => {
setError(err);
})
.finally(() => {
setloading(false);
});
};
useEffect(() => {
fetchData();
}, [method, url, body, headers]);
return { response, error, loading };
};
export default useAxios;
这是登录组件。
import React from 'react';
import { Form, Input, Button } from 'antd';
import useAxios from '../hooks/useAxios';
const LoginForm = () => {
const axios = useAxios;
const onFinish = (body) => {
const test = axios({
url: 'api/auth/login',
method: 'post',
body,
});
console.log(test);
};
const findIDAndPassword = () => {
console.log('findIDAndPassword');
};
return (
<Form name="basic" onFinish={onFinish} autoComplete="off">
<Form.Item
name="id"
rules={[
{
required: true,
message: 'Please input your id!',
},
]}
>
<Input placeholder="ID" />
</Form.Item>
<Form.Item
name="password"
rules={[
{
required: true,
message: 'Please input your password!',
},
]}
>
<Input.Password placeholder="PASSWORD" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit" block>
login
</Button>
</Form.Item>
<Button type="link" block onClick={findIDAndPassword}>
findIDAndPassword
</Button>
</Form>
);
};
export default LoginForm;
但它给了我这个错误。如何解决?
src\components\LoginForm.js
Line 7:18: React Hook "useAxios" is called in function "onFinish" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks
Search for the keywords to learn more about each error.
【问题讨论】:
标签: javascript reactjs