React 画面切り替えができない
Q&A
Closed
解決したいこと
Reactで画面切り替えを行いたいが、検証ツールで見たところエラーになってしまう問題を解消したい
発生している問題・エラー
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.
Uncaught TypeError: Cannot read properties of null (reading 'useRef')
at Object.useRef (react.development.js:1630:1)
at BrowserRouter (index.tsx:772:1)
at renderWithHooks (react-dom.development.js:16305:1)
at mountIndeterminateComponent (react-dom.development.js:20074:1)
at beginWork (react-dom.development.js:21587:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at beginWork$1 (react-dom.development.js:27451:1)
at performUnitOfWork (react-dom.development.js:26557: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.
Uncaught TypeError: Cannot read properties of null (reading 'useRef')
at Object.useRef (react.development.js:1630:1)
at BrowserRouter (index.tsx:772:1)
at renderWithHooks (react-dom.development.js:16305:1)
at mountIndeterminateComponent (react-dom.development.js:20074:1)
at beginWork (react-dom.development.js:21587:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
at invokeGuardedCallback (react-dom.development.js:4277:1)
at beginWork$1 (react-dom.development.js:27451:1)
at performUnitOfWork (react-dom.development.js:26557:1)
The above error occurred in the <BrowserRouter> component:
at BrowserRouter (http://localhost:3000/static/js/bundle.js:70782:5)
at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
Uncaught TypeError: Cannot read properties of null (reading 'useRef')
at Object.useRef (react.development.js:1630:1)
at BrowserRouter (index.tsx:772:1)
at renderWithHooks (react-dom.development.js:16305:1)
at mountIndeterminateComponent (react-dom.development.js:20074:1)
at beginWork (react-dom.development.js:21587:1)
at beginWork$1 (react-dom.development.js:27426:1)
at performUnitOfWork (react-dom.development.js:26557:1)
at workLoopSync (react-dom.development.js:26466:1)
at renderRootSync (react-dom.development.js:26434:1)
at recoverFromConcurrentError (react-dom.development.js:25850:1)
NameError (uninitialized constant World)
該当するソースコード
import './App.css';
import Header from './Header';
import Main from './Main';
import Proggramming from './Proggramming'
import Sns from './Sns'
import Touhou from './Touhou'
import { BrowserRouter, Link, Routes, Route } from "react-router-dom";
function App() {
return (
<>
<div>dd </div>
<BrowserRouter>
<div id='main'>
<Link to='/'>Home</Link>
<br />
<Link to='/Proggramming'>Proggramming</Link>
<br />
<Link to='/Sns'>Page2</Link>
<br />
<Link to='/Touhou'>Page3</Link>
<br />
<Header />
<Routes>
<Route exact path='/' element={<Main />}>
<Main />
</Route>
<Route path='/proggramming' element={<Proggramming />}>
<Proggramming />
</Route>
<Route path='/sns' element={<Sns />}>
<Sns />
</Route>
<Route path='/touhou' element={<Touhou />}>
<Touhou />
</Route>
</Routes>
</div>
</BrowserRouter>
</>
);
}
export default App;
自分で試したこと
useRefやフックスでなにか問題が起きたとエラーを解釈したのですが、私はこれらの機能を使った覚えがないです。そのため調べてもよくわからず、皆さんのお力添えをお願いした次第です。
お時間があれば宜しくお願い致しますm(_ _)m