@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()
);
  • Вопрос задан
  • 356 просмотров
Решения вопроса 1
Alexandroppolus
@Alexandroppolus
кодир
root.render(
  <App />
);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы