1

I am getting below invalid hook call error in React when I used materidal design bootstrap component in my application

Error: 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

This is my header component

import React, { useState } from "react";
import {
  MDBNavbar,
  MDBNavbarBrand,
  MDBNavbarNav,
  MDBNavItem,
  MDBNavLink,
  MDBNavbarToggler,
  MDBCollapse,
  MDBFormInline,
  MDBDropdown,
  MDBDropdownToggle,
  MDBDropdownMenu,
  MDBDropdownItem,
} from "mdbreact";

const Header = () => {
  return (
    <>
      <MDBNavbar color="indigo" dark expand="md">
        <MDBNavbarBrand>
          <strong className="white-text">Navbar</strong>
        </MDBNavbarBrand>
        <MDBNavbarToggler />
        <MDBCollapse id="navbarCollapse3" navbar>
          <MDBNavbarNav left>
            <MDBNavItem active>
              <MDBNavLink to="#!">Home</MDBNavLink>
            </MDBNavItem>
            <MDBNavItem>
              <MDBNavLink to="#!">Features</MDBNavLink>
            </MDBNavItem>
            <MDBNavItem>
              <MDBNavLink to="#!">Pricing</MDBNavLink>
            </MDBNavItem>
            <MDBNavItem>
              <MDBDropdown>
                <MDBDropdownToggle nav caret>
                  <span className="mr-2">Dropdown</span>
                </MDBDropdownToggle>
                <MDBDropdownMenu>
                  <MDBDropdownItem href="#!">Action</MDBDropdownItem>
                  <MDBDropdownItem href="#!">Another Action</MDBDropdownItem>
                  <MDBDropdownItem href="#!">
                    Something else here
                  </MDBDropdownItem>
                  <MDBDropdownItem href="#!">
                    Something else here
                  </MDBDropdownItem>
                </MDBDropdownMenu>
              </MDBDropdown>
            </MDBNavItem>
          </MDBNavbarNav>
          <MDBNavbarNav right>
            <MDBNavItem>
              <MDBFormInline waves>
                <div className="md-form my-0">
                  <input
                    className="form-control mr-sm-2"
                    type="text"
                    placeholder="Search"
                    aria-label="Search"
                  />
                </div>
              </MDBFormInline>
            </MDBNavItem>
          </MDBNavbarNav>
        </MDBCollapse>
      </MDBNavbar>
    </>
  );
};

export default Header;

App.js

import logo from './logo.svg';
import './App.css';
import Header from './components/Header';

function App() {
  return (
    <div className="App">
       <Header />
    </div>
  );
}

export default App;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import '@fortawesome/fontawesome-free/css/all.min.css';
import'bootstrap-css-only/css/bootstrap.min.css';
import'mdbreact/dist/css/mdb.css';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();
James
  • 371
  • 3
  • 12
  • can you share where you have defined the state; the error dictates `Hooks can only be called inside of the body of a function component` thereby if you have defined the hooks outside of functional component that could possibly be the issue. – Chinmay Dali Jul 21 '21 at 15:15
  • I haven't use any state in my component, The one you see in Header.js it just declaration. No state has been used in any component – James Jul 21 '21 at 16:33

1 Answers1

0

In your component their are navlinks which need to inside by <Router> component as shown in the documentation.

import { BrowserRouter as Router } from "react-router-dom";

import React, { useState } from "react";
const Header = () => {
  return (
    <>
      <Router>
        <MDBNavbar color="indigo" dark expand="md">
          .
          .
          .
        </MDBNavbar>
      </Router>
    </>
  );
};

codesandbox link for the code

Chinmay Dali
  • 375
  • 5
  • 14
  • I tried that way as well. But I got the same issue again. Absolutely no idea what's going on. The sandbox working fine but in my VS code it's not working – James Jul 21 '21 at 18:23
  • mdbReact is not upgraded with React 17. That's the reason I am getting all the issue. After degrading react version it's worked fine – James Jul 21 '21 at 18:35
  • Add your answer stating the reason and how you fixed it thereby it would be easier for the rest of the members in the community. – Chinmay Dali Jul 22 '21 at 06:50
  • Partial Working. I am able to see navbar component but I am not able to navigate between pages – James Jul 22 '21 at 12:39