'useState' is not defined React JS – Using it with Router


I am trying to add the dark mode, but currently facing a problem in which compiler shows that

src/App.js Line 16:36: ‘useState’ is not defined no-undef Line
17:48: ‘useState’ is not defined no-undef Line 18:58: ‘useState’
is not defined no-undef

I don’t understand why I am getting such error because I have passed the values using props. can someone check and tell me what’s wrong and explain me a bit of concept?

Thanks in advance.


import './App.css';
import React from 'react'
import Form from './Components/Form';
import Message from './Components/Message'
import Home from './Components/Home'
import 'bootstrap/dist/css/bootstrap.min.css';
import Headers from './Components/Header';
import UpperCase from './Components/UpperCase';
import {
  BrowserRouter as Router,
} from "react-router-dom";

function App() {
  const[darkMode,darkModeChange] = useState('light');
  const[buttonText,darkModeChangeTextButton] = useState('Enable Dark Mode');
  const[buttonTextStyle,darkModeChangeTextButtonStyle] = useState({color:'fff'});
  const darkModeOnChange = () => {
    if(darkMode === 'light'){
   darkModeChangeTextButton('Disable Dark Mode');
   darkModeChangeTextButtonStyle({color: '#fff'});
   document.body.style.backgroundColor = '#020046';
   darkModeChangeTextButton('Enable Dark Mode');
   darkModeChangeTextButtonStyle({color: '#212529'});
   document.body.style.backgroundColor = '#fff';
  return (
      <Headers buttonText={buttonText} buttonTextStyle={buttonTextStyle} mode={darkMode} changeMode={darkModeOnChange}></Headers>
         <Route path="/" element={<Home />}>
        <Route path="/home" element={<Home />}>
        <Route path="/form" element={<Form />}>
        <Route path="/message" element={<Message />}>
        <Route path="/UpperCase" element={<UpperCase />}>

    </div >

export default App;


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

function Headers(props){

    return (
       <nav className={`navbar navbar-expand-lg navbar-${props.darkMode} bg-${props.darkMode}`}>
  <div className="container-fluid">
    <Link className="navbar-brand" to="/home">Sanad's Blog</Link>
    <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
    <div className="collapse navbar-collapse" id="navbarSupportedContent">
      <ul className="navbar-nav me-auto mb-2 mb-lg-0">
        <li className="nav-item">
          <Link className="nav-link active" aria-current="page" to="/home">Home</Link>
        <li className="nav-item">
          <Link className="nav-link" to="/form">Form</Link>
        <li className="nav-item">
          <Link className="nav-link" to="/message" aria-disabled="true">Message</Link>
        <li className="nav-item">
          <Link className="nav-link" to="/UpperCase">Upper Case Tool</Link>
      <div className="form-check form-switch">
  <input className="form-check-input" type="checkbox" id="flexSwitchCheckDefault"onChange={props.darkModeOnChange} />
  <label style={props.buttonTextStyle} className="form-check-label" htmlFor="flexSwitchCheckDefault">{props.buttonText}</label>
export default Headers


after importing the { useState } in App.js and removed from Header.js

I am not able to get props values in Header.js
Props values are getting rendered as Undefined
why is that?

>Solution :

You forgot to import the useState method in the App.js line #2

Please replace in App.js the line:

import React from 'react'


import React, { useState } from "react";

This is why the App.js complained about the undefined useState.

Leave a Reply Cancel reply