Задать вопрос
@imagance

В чем проблема с хуками в react?

У меня выводит ошибку с хуком в react. Помогите, пожалуйста.
Ошибка 1:
Warning: 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.

Ошибка 2:
Uncaught TypeError: Cannot read properties of null (reading 'useState')
at useState (react.development.js:1622:1)
at App (App.js:6:1)
at ./src/index.js (index.js:8:1)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at startup:7:1
at startup:7:1

App.js
import logo from './logo.svg';
import React, {useState} from 'react';

function App() {

  const [likes, setLike] = useState(0)

  function increment() {
    setLike(likes + 1)
  }
  function shit() {
    setLike(likes - 1)
  }
  return (
    <div className='App'>
    <h1>{likes}</h1>
    <button onclick={increment}>sd</button>
    <button onclick={shit}>sd</button>
  </div>
  );
}

export default App;


index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  App()
);
  • Вопрос задан
  • 385 просмотров
Подписаться 1 Простой Комментировать
Помогут разобраться в теме Все курсы
  • Яндекс Практикум
    Мидл фронтенд-разработчик
    5 месяцев
    Далее
  • Javascript.ru
    Курс по React
    5 недель
    Далее
  • Нетология
    Fullstack-разработчик на Python + нейросети
    20 месяцев
    Далее
Решения вопроса 1
Alexandroppolus
@Alexandroppolus
кодир
root.render(
  <App />
);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы
ITK academy Нижний Новгород
от 50 000 до 90 000 ₽
IT ATLAS Москва
от 200 000 до 250 000 ₽
ITK academy Казань
от 50 000 до 90 000 ₽