vladosdc
@vladosdc
Frontend Developer

Почему возникает такая ошибка NEXT JS 13?

Подскажите пожалуйста почему в Next 13 такая ошибка:

Unhandled Runtime Error
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Source
src\services\register.tsx (7:28) @ state

5 |
6 | const RegisterService = () => {
> 7 | const email = useSelector(state => state.email)
| ^
8 | const password = useSelector(state => state.password)
9 |
10 | createUserWithEmailAndPassword(auth, email, password)

Вот код:

"use client"
import { createUserWithEmailAndPassword } from "firebase/auth";
import {auth} from '../firebase/firebase'
import {useSelector} from "react-redux";

const RegisterService = () => {
const email = useSelector(state => state.email)
const password = useSelector(state => state.password)

createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;

})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});

}

export default RegisterService
  • Вопрос задан
  • 223 просмотра
Решения вопроса 1
Aetae
@Aetae
Тлен
Как ты используешь этот RegisterService?
Чтоб он работал, как и написано в ошибке, он должен быть компонентом.
Т.е. <RegisterService ...> или React.createElement(RegisterService, ...) .

Второй вариант: он может быть не компонентом, а хуком. Тогда он должен вызываться внутри другого компонента, а также иметь префикс use - useRegisterService(отсутствие префикса не приведёт к ошибке, но это стандарт).

Если ты просто вызываешь его где-то вне компонента как функцию - ничего работать не будет.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
06 мая 2024, в 09:38
1200 руб./в час
06 мая 2024, в 09:37
1200 руб./в час
06 мая 2024, в 09:36
8000 руб./за проект