• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    해결됨

Warning: Invalid hook call 오류

23.06.08 21:18 작성 조회수 292

0

react.development.js:209 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.
이런 오류가 뜨면서 브라우저 렌더링이 안됩니다. 

스크린샷 2023-06-08 오후 6.20.36.png

1. 이걸 보면 호환문제는 아닌 것 같고,

 

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import store from './store';
import { BrowserRouter } from 'react-router-dom';


const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
</React.StrictMode>
);

 

 

import './App.css';

function App() {
return (
<div className="App">
</div>
 );
}

export default App;

 

import {createSlice, configureStore} from '@reduxjs/toolkit'

//state 만들기
let hobby = createSlice({
name: 'hobby',
initialState: 'dancing'
})

//state 등록하기
export default configureStore({
reducer:{
hobby : hobby.reducer
/*작명: createSlice만든거.reducer */
 }
})

 

import {useSelector} from "react-redux";

function Cart(){
let a = useSelector((state)=> state.hobby)
console.log(a)
return(a)
}

 

 

전체 코드입니다. 2번처럼 hook 규칙을 어긴것도 아닌것 같고, 3번 중복 설치 문제일까요??

스크린샷 2023-06-08 오후 6.23.28.png

 

정말 찾아도 찾아도 모르겠어요

 

답변 2

·

답변을 작성해보세요.

0

정지은님의 프로필

정지은

질문자

2023.06.08

콘솔 오류창을 더 내려보니 이런 문구도 있었습니다. 오류를 더 자세히 읽어야 겠어요!

react-dom.development.js:18687 The above error occurred in the <BrowserRouter> component:

at BrowserRouter (http://localhost:3000/static/js/bundle.js:46950:5) at Provider (http://localhost:3000/static/js/bundle.js:35586:5) 

0

정지은님의 프로필

정지은

질문자

2023.06.08

해결했습니다.
<BrowseRouter>를 설치하지 않고 작성한 것이 문제가 되었습니다

import만 하면 vscode 상으로는 문제가 없어서, 간과했습니다.

이 오류는 공식문서에 나온 방법으로는 해결이 불가능해서, 혹시 저처럼 리액트라우터를 설치하지 않고, <BrowseRouter>를 작성했다가 페이지를 렌더링을 하는데 오류를 가진 사람들을 위해서 블로그에 포스팅을 하겠습니다.
읽어주셔서 감사합니다.
코드 한줄을 쓰더라도 왜 이 코드를 써야하는지 생각하면서 작성하겠습니다