reactjsthree.jstailwind-css

Error message "Uncaught TypeError: Cannot destructure property 'basename' of 'React2.useContext(...)' as it is null"


I'm facing

Uncaught TypeError: Cannot destructure property 'basename' of 'React2.useContext(...)' as it is null.

In the Link component of the code:

import React, { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';

import { styles } from '../styles';
import { navLinks } from '../constansts';
import { logo, menu, close } from '../assets';

const Navbar = () => {
  const [active, setActive] = useState('');
  return (
    <nav className={`${styles.paddingX} w-full flex items-center py-5 fixed top-0 z-20     bg-primary`}>
      <div className="w-full flex justify-between items-center max-w-7x1 max-auto">
        <Link
          to="/"
          className="flex items-center gap-2"
          onClick={() => {
            setActive('');
            window.scrollTo(0, 0);
          }}
        >
          <img alt="logo" />
        </Link>
      </div>
    </nav>
  );
};

export default Navbar;

How can I fix it?


Solution

  • You're using Link from react-router-dom, which is calling useContext. The context it is looking for is provided by BrowserRouter, but your app is not wrapped by a BrowserRouter. Your index.js:

    import { BrowserRouter } from 'react-router-dom'
    
    render(
      <BrowserRouter>
        <App />
      </BrowserRouter>,
      document.getElementById('root')
    )