简体   繁体   English

反应-导出上下文提供程序给我错误:“元素类型无效:应为字符串[…],但得到:未定义”

[英]React - Exporting Context Provider gives me error: “Element type is invalid: expected a string […] but got: undefined”

Summary 摘要

I have a file where I create a component with a React Context Provider, which I exported to another file (see code). 我有一个文件,可在其中使用React Context Provider创建组件,并将其导出到另一个文件(请参见代码)。 But when Importing it it gives me an error. 但是导入时给我一个错误。

Already tried different methods of importing the file. 已经尝试了导入文件的其他方法。 But get the same error. 但是得到同样的错误。 Logging the imported Provider in the component shows that it exists. 在组件中记录导入的Provider,表明它存在。

Code

The exported file 导出的文件

function PositionContextProvider({ children }) {
  return <EditModeContext.Provder value={false}>{children}</EditModeContext.Provder>;
}

export { PositionContextProvider };

The import 进口

import { PositionContextProvider } from './PositionContext';

[...]

function Application() {
  return (
   <ContextQuery query={getPositions}>
    {({ data, loading, fetchMore, isFetchingMore }) => {
      return (
        <PositionContextProvider>
          ...
        </PositionContextProvider /> 
      )
    })}
   </ContextQuery />
  )
}

Expected to be able to use the context in any of the components within the Provider but get the error message in the console. 期望能够在Provider中的任何组件中使用上下文,但会在控制台中获得错误消息。

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. 您可能忘记了从定义文件中导出组件,或者可能混淆了默认导入和命名导入。

assign your function to variable then export it: 将函数分配给变量,然后将其导出:

const PositionContextProvider = function({ children }) {
  return <EditModeContext.Provder value={false}>{children}</EditModeContext.Provder>;
}

export { PositionContextProvider };

错字:提供商中缺少“ i”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native Element类型无效,预期字符串但未定义 - React Native Element type is invalid expected a string but got undefined React Memo功能给出: - 未捕获的错误:元素类型无效:期望一个字符串但得到:object - React Memo Feature gives :- Uncaught Error: Element type is invalid: expected a string but got: object 错误:缩小的 React 错误 #130:元素类型无效:预期为字符串或类/函数,但得到:未定义 - Error: Minified React error #130: Element type is invalid: expected a string or a class/function but got: undefined 反应错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React Native 错误:元素类型无效:应为字符串或类/函数,但得到:未定义 - React Native error! Element type is invalid: expected a string or a class/function but got: undefined 错误:元素类型无效:应为字符串(用于内置组件)]但得到:未定义。 Nextjs,反应 - Error: Element type is invalid: expected a string (for built-in components) ]but got: undefined. Nextjs, React React Native 错误:元素类型无效:应为字符串或类/函数,但得到:未定义。 但是哪里? - React Native Error: Element type is invalid: expected a string or a class/functionbut got: undefined. But where? React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React Native 错误:元素类型无效:应为字符串或类/函数(对于复合组件)但得到:未定义 - React Native Error: Element type is invalid: expected a string or a class/function (for composite components) but got: undefined 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 反应 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM