skip to Main Content

I am facing an error like this everything works fine, it’s compiled successfully but the app does not display on the browser and appears:
"Uncaught Runtime error: Cannot read properties of null (reading ‘useRef’)"

This is my code:

import Navbar from './components/Navbar'
import {BrowserRouter as Router, Route, Routes } from 'react-router-dom'

function App() {

  return (
    <div className="App">
      <Router>
        <Navbar />
        <Routes>
          <Route></Route>
        </Routes>
      </Router>
    </div>
  );
}

export default App;

before using this: "import {BrowserRouter as Router, Route, Routes } from ‘react-router-dom’" it was all fine. Now, If I remove this import and Router, Routes, Route, it works fine.
I also installed react-router-dom but the error remains. I was starting to build the app but I received this error.

this is the error that appears on browser

2

Answers


  1. Chosen as BEST ANSWER

    I have solved my problem. Firstly I used the command npm install react-hook-form but the error remains after using this.. Then I just opened a PowerShell window in the root folder of my app where I have package.json, src and other files and then I used npm install react-router-dom and it added 3 packages. Now it's working well and good. I hope it helps others having the same issue.


  2. It seems like the error is related to the use of the useRef hook and may be tied to the introduction of react-router-dom. Without seeing your code, I can provide some general guidance on how to address this issue:

    Check React and React Router Versions:

    Ensure that you have compatible versions of React and React Router. In some cases, certain features or hooks might not be available in older versions.

    npm list react react-dom react-router-dom
    

    Make sure you’re using React Router v6 if you’ve imported Routes and Route directly.

    Import Statements:

    Double-check your import statements. Ensure that you are importing the necessary components and hooks correctly.

    import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
    import { useRef } from 'react';
    Recheck the Code Using useRef:
    

    Examine the code where you are using useRef. Ensure that you are using it correctly and that the referenced element exists in the component tree.

    const myRef = useRef(null);
    // ... rest of the component code
    

    Component Rendering:

    Check how your components are being rendered inside the Routes component. Ensure that there are no issues with the rendering logic.

    <Routes>
    <Route path="/" element={<YourComponent />} />
    {/* ... other routes */}
    </Routes>
    

    Browser Console:

    Open your browser’s developer tools (usually by pressing F12), go to the "Console" tab, and check if there are any additional error messages that might provide more details about the issue.
    Component Hierarchy:

    Make sure that the component using useRef is rendered within the component tree covered by the Router. Components outside the Router won’t have access to routing-related hooks.
    If the issue persists, consider providing relevant portions of your code (especially where you use useRef and the routing components) so that a more specific solution can be offered.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search