Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't know why I am getting error while exporting FirstName?

import React, { createContext } from 'react';

import './App.css';

import ComA from './ComA.jsx';

function App() {

  const FirstName = createContext();

  return (
    <>
      <FirstName.Provider value={'Ganesh'}>

        <ComA />

      </FirstName.Provider>
    </>
  );
}

export default App;


export { FirstName };

output:

ERROR in \[eslint\]
src\\App.js
Line 28:9:  Parsing error: Export 'FirstName' is not defined. (28:9)

webpack compiled with 2 errors

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error message suggests that you are trying to export something that has not been defined. In this case, you are trying to export the FirstName context, but it has only been defined within the App component.

To fix this error, you can move the createContext outside of the App component and export it directly. Here is an example:

import React, { createContext } from 'react';
import './App.css';
import ComA from './ComA.jsx';

export const FirstNameContext = createContext();

function App() {
  return (
    <FirstNameContext.Provider value={'Ganesh'}>
      <ComA />
    </FirstNameContext.Provider>
  );
}

export default App;

With this change, you can now import the FirstNameContext in other files:

import React, { useContext } from 'react';
import { FirstNameContext } from './App';

function ComA() {
  const firstName = useContext(FirstNameContext);

  return (
    <div>
      My name is {firstName}.
    </div>
  );
}

export default ComA;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading