How to Solve the “‘#’ is not defined react/jsx-no-undef” Error in React?

The “‘#’ is not defined react/jsx-no-undef” error in React frequently occurs when trying to use components but forgetting to import them or misspelling some of the components’ names. Following the explanation below to understand better.

What causes the error “‘#’ is not defined react/jsx-no-undef”?

Forget to import the component

// The component is not imported
import React from 'react';

function App() {
  return (
    <div className='App'>
      <h1>Welcome to Learnshareit</h1>

      {/* Forget to import the Counter component */}
      <Counter />
    </div>
  );
}

export default App;

Output

Misspelling the component name

// The component is not imported
import React from 'react';
import counter from './Counter'; // Spelling mistake in the component name

function App() {
  return (
    <div className='App'>
      <h1>Welcome to Learnshareit</h1>

      <Counter />
    </div>
  );
}

export default App;

Here, we have imported the component, but it must be the correct spelling. It should be “Counter” not “counter“.

Notice in React, the component should start with a capital letter to distinguish it from a standard HTML tag.

Output

How to solve this error?

The following can help you solve the error:

  • Make sure to import the component you want to use.
  • If it is imported, check the spelling of the component name (it should begin with a capital letter).

Here, the error is resolved when you following the rules above:

import React from 'react';
import Counter from './Counter';

function App() {
  return (
    <div className='App'>
      <h1>Welcome to Learnshareit</h1>

      <Counter />
    </div>
  );
}

export default App;
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  
  return (
    <div className='counter'>
      Count number: {count}
      <div>
        <button onClick={() => setCount(count + 1)} style={{ marginRight: 5 }}>
          Increment
        </button>
        <button onClick={() => setCount(count - 1)}>Decrement</button>
      </div>
    </div>
  );
}

export default Counter;

Output

Summary

This article is about solving the “‘#’ is not defined react/jsx-no-undef” error in React. We hope reading this article helps you solve it. Also, we recommend using tools like create-react-app or vite to create a React application because it integrates some plugins to help you detect errors quickly, therefore, can program better. You can learn more about how to create a React application here. If you have any questions, please feel free to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *