본문 바로가기
에러 해결

[React] error: useRoutes() may be used only in the context of a <Router> component.

by codnjs779 2021. 11. 27.

 

페이지 라우트 처리를 다 완료 했는데 이런 에러가 발생했다.

최종적으로 src index.js 에 BrowserRouter 처리를 해주지 않았기 때문에 발생한 문제이다. 

 

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";

import App from "./components/App";
import reportWebVitals from "./reportWebVitals";

import { Provider } from "react-redux";
import { store } from "../src/redux/store";

ReactDOM.render(
    <Provider store={store}>
        <BrowserRouter>
            <App />{" "}
        </BrowserRouter>
    </Provider>,
    document.getElementById("root")
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

이렇게 수정해주면 오류가 사라진다.