A
A
Alexey Yakovlev2022-01-04 15:21:41
Node.js
Alexey Yakovlev, 2022-01-04 15:21:41

Error sending data to server?

Mistake:

Uncaught 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.
    at resolveDispatcher (react.development.js:1476)
    at Object.useContext (react.development.js:1484)
    at useTheme (useTheme.js:4)
    at useTheme (useThemeWithoutDefault.js:8)
    at useTheme (useTheme.js:6)
    at useThemeProps (useThemeProps.js:8)
    at useThemeProps (useThemeProps.js:7)
    at Alert (Alert.js:124)
    at renderWithHooks (react-dom.development.js:14985)
    at updateForwardRef (react-dom.development.js:17044)


Action to send data:
import { SetStateAction, Dispatch } from "react";
import {IUserDefaultInfo, IMessage} from "../interfaces";

export const register = async(user:IUserDefaultInfo, setMessage: Dispatch<SetStateAction<IMessage>>) => {
    try {
        const response = await fetch("http://localhost:5000/api/auth/register", {
            method: "POST",
            body: JSON.stringify(user),
            headers: {
                "Content-Type": "application/json"
            }
        })

        response.json().then(data => {
            if (!response.ok) {
                setMessage({
                    text: data.message,
                    type: "error"
                });
            } else {
                setMessage({
                    text: data.message,
                    type: "success"
                });
            }
        })
    } catch(e:any) {
        console.log(e);
        setMessage({
            text: e.message,
            type: "error"
        });
    }
}


Component where I call it:
import React from 'react';
import { NavLink } from 'react-router-dom';
import { register } from '../actions/Auth';
import MyInput from '../components/input/myInput';
import MySubmit from '../components/submit/mySubmit';
import {IMessage, IUserDefaultInfo} from "../interfaces";
import Alert from '@mui/material/Alert';

const Register: React.FC = () => {
    const [user, setUser] = React.useState<IUserDefaultInfo>({
        name: "", email: "", password: ""
    });
    const [message, setMessage] = React.useState<IMessage>({
        text: "", type: "success"
    })

    return (
        <div className="register">
            <div className="container">
                {message.text && <Alert severity={message.type}>{message.text}</Alert>}
                <h2 className="title">Регистрация</h2>

                <div className="form">
                    <div className="form__input">
                        <label htmlFor="name" className="form__label">Имя пользователя</label>
                        <MyInput
                            type="text" 
                            id="name"
                            value={user.name}
                            onChange={(event: React.ChangeEvent<HTMLInputElement>) => setUser({...user, name: event.target.value})}
                        />
                    </div>
                    <div className="form__input">
                        <label htmlFor="email" className="form__label">Электронная почта</label>
                        <MyInput
                            type="email"
                            id="email"
                            value={user.email}
                            onChange={(event: React.ChangeEvent<HTMLInputElement>) => setUser({...user, email: event.target.value})}
                        />
                    </div>
                    <div className="form__input">
                        <label htmlFor="password" className="form__label">Пароль</label>
                        <MyInput
                            type="password"
                            id="password"
                            value={user.password}
                            onChange={(event: React.ChangeEvent<HTMLInputElement>) => setUser({...user, password: event.target.value})}
                        />
                    </div>

                    <MySubmit onClick={() => register(user, setMessage)}>Зарегистрироваться</MySubmit>
                </div>

                <p className="text">
                    Есть аккаунт? <NavLink to="/login">Войти</NavLink>
                </p>
            </div>
        </div>
    )
}

export default Register;


I understand that the problem is in the hooks (where I call them), but I can’t understand where and which hook I called in the wrong place.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gsaw, 2022-01-04
@gsaw

Read here
https://ru.reactjs.org/docs/hooks-rules.html#only-... Hooks
cannot be called from normal javascript functions
https://ru.reactjs.org/docs/hooks-custom.html
https ://ru.reactjs.org/docs/hooks-rules.html
I was just looking into them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question