无效钩子调用.React.js



从react中导入useState,粘贴到body函数中,但是react告诉我错误地使用钩子。尽管另一个钩子是这样工作的。

import {getAuth} from 'firebase/auth';
import {useState} from 'react';
const useAuth = () => {
const [state, setState] = useState(null); //wrong use
const auth = getAuth();
const getUserId = () => {
return new Promise((resolve, reject) => {
auth.onAuthStateChanged((user) => {
if (user) {
resolve(user.uid);
} else {
resolve(null);
}
});
});
};
return {
getUserId,
};
};
export default useAuth;

您返回的getUserId不是jsx。如果你的函数打算成为一个react功能组件,那么它应该返回一个jsx。如果没有,你就不能使用像useState这样的react钩子。