I'm encountering an error where everything seems to be fine - the code compiles successfully - but the app isn't displaying in the browser. Instead, I'm seeing this error message: 'Uncaught Runtime error: Cannot read properties of null (reading 'useRef')'. I'm working on a React project, and everything was functioning properly until I integrated react-router-dom. Now, the router isn't functioning as expected. I'm attempting to navigate to the home screen, but instead of displaying it, I'm receiving error.

So in this post, I'm going to discuss that error and its solution.

Solution to "TypeError: Cannot read properties of null (reading 'useRef')"

This error usually occurs when trying to access properties or methods of a null object, typically within a React component. In the case of 'useRef', it commonly happens when attempting to use a ref on a component that hasn't been properly initialized or mounted yet.

Possible Reason:

  1. The component accessing 'useRef' is not yet mounted.
  2. There might be an issue with the way components are rendered or nested.
  3. Incorrect handling of asynchronous operations.

Solution:

To resolve this error, you can ensure that the component is properly mounted before accessing useRef. Here's an example react code:


    import React, { useRef, useEffect } from 'react';

    const MyComponent = () => {
      const myRef = useRef(null);

      useEffect(() => {
        // Accessing myRef safely after component is mounted
        console.log(myRef.current);
      }, []);

      return (
        
{/* Component content */}
); } export default MyComponent;

In above example, we use the useEffect hook to ensure that the ref is accessed only after the component is mounted. By passing an empty dependency array [] to useEffect, we ensure that the effect runs only once, after the initial render.


2

We encountered the same error because we didn't install 'React Hook Form' in the correct folder. It's essential to ensure that it's listed in your package.json. If the issue persists, you can try the following solution, especially helpful for those who have recently started working with Next.js.

While using react-hook-form with Next.js release, we faced a similar problem. Our initial configuration looked like this:

const nextConfig = {
  experimental: {
    appDir: true,
  },
}

 It appears that the experimental appDir feature might have been causing conflicts with the setup, particularly when used in conjunction with react-hook-form. By setting appDir to false, we essentially disable this experimental feature, which seems to solve the problem.

3

You need to check our package.json file to ensure that every library you're using is listed either as "dependencies" or "devDependencies". If any are missing, you should install them individually.

In our case, if we're not seeing 'react-router-dom', open a terminal in the root folder of our project, where package.json is located, and run:

npm install react-hook-form

If the problem persists, you should check that our Node.js version is not higher than the last recommended one. If it is, you can downgrade it using the 'n' package from npm:

# If one of the commands does not pass, you may need to use sudo
npm i -g n
n stable
# Delete node_modules and start over
rm -rf node_modules
npm install

Finally, you need to ensure that all our components start with a capital letter, such as 'ProductScreen' instead of 'productScreen'. If you're using version 6 of React Router Dom, you should change the 'component' property of Route to 'element'. 

Alternatively, you can verify that your 'react-router-dom' dependency is installed correctly. If you've already installed it and your app was working perfectly before, but the error occurred after some days, you should check our package.json and reinstall 'react-router-dom' using:

npm i react-router-dom


4

Firstly, I tried using the command npm install react-hook-form, but unfortunately, the error persisted even after doing so. Then, I took a different approach. I opened a PowerShell window in the root folder of my app, where I have package.json, src, and other files, and ran npm install react-router-dom. This command added three packages. After that, my app started working perfectly fine. I believe this solution could be helpful to others experiencing the same issue.

However, even after applying this method, I encountered the same error again. Upon further investigation, I realized that the problem was caused by installing react-router-dom outside the folder where my app is located. 

Consequently, the browser router wasn't accessible. To resolve this, I navigated to the folder containing my React app and then reinstalled react-router-dom. This simple step resolved the issue for me. 

5

I just wanted to share that I encountered this error, and upon investigation, I realized that I was calling useRef outside of the component. It's crucial to remember that hooks cannot be called outside of a functional component. Moving the useRef call inside the component resolved the issue for me.