반응형
<Error> useRoutes() may be used only in the context of a <Router> component.
react-router-dom 라이브러리를 import하여 Route, Routes 등을 사용하려 할 때 발생하는 에러
해당 라이브러리를 사용하기 위해서는 index 파일에서 App 컴포넌트를 <BrowserRouter> 컴포넌트로 감싸주어야 한다.
index.js / index.tsx (전)
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') as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
index.js / index.tsx (후)
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from "react-router-dom";
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<BrowserRouter>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
);
reportWebVitals();반응형
'개발 및 알고리즘 > 개발' 카테고리의 다른 글
| [React Native] M3 Macbook Pro에 리액트 네이티브(React-Native-Cli) 초기 세팅하기 (개발 환경 구축부터 Android, iOS 기기 Build까지) (3) | 2024.04.16 |
|---|---|
| [Git] git commit message 작성 및 적용하기 (commit convention) (0) | 2024.04.01 |
| [Redux] Redux를 사용하는 이유 (0) | 2024.03.30 |
| [React] 리액트 스타일링 (css module, classnames) (0) | 2024.03.29 |
| [Git] 프로젝트 폴더 연동하기 (0) | 2024.03.27 |