我不知道为什么在导出 FirstName 时出现错误。

huangapple go评论145阅读模式
英文:

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 };
英文:
import React, { createContext } from &#39;react&#39;;

import &#39;./App.css&#39;;

import ComA from &#39;./ComA.jsx&#39;;

function App() {

  const FirstName = createContext();

  return (
    &lt;&gt;
      &lt;FirstName.Provider value={&#39;Ganesh&#39;}&gt;

        &lt;ComA /&gt;

      &lt;/FirstName.Provider&gt;
    &lt;/&gt;
  );
}

export default App;


export { FirstName };

output:

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

webpack compiled with 2 errors

答案1

得分: 1

以下是代码部分的中文翻译:

错误信息表明,您正在尝试导出尚未定义的内容。在这种情况下,您正尝试导出FirstName上下文,但它仅在App组件中已定义。

要修复此错误,您可以将createContext移出App组件并直接导出它。以下是一个示例:

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;

通过这个更改,您现在可以在其他文件中导入FirstNameContext:

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

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

  return (
    <div>
      我的名字是 {firstName}
    </div>
  );
}

export default ComA;
英文:

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 &#39;react&#39;;
import &#39;./App.css&#39;;
import ComA from &#39;./ComA.jsx&#39;;

export const FirstNameContext = createContext();

function App() {
  return (
    &lt;FirstNameContext.Provider value={&#39;Ganesh&#39;}&gt;
      &lt;ComA /&gt;
    &lt;/FirstNameContext.Provider&gt;
  );
}

export default App;

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

import React, { useContext } from &#39;react&#39;;
import { FirstNameContext } from &#39;./App&#39;;

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

  return (
    &lt;div&gt;
      My name is {firstName}.
    &lt;/div&gt;
  );
}

export default ComA;

huangapple
  • 本文由 发表于 2023年4月11日 14:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定