[英]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.