我通过反应钩子和函数获得了住宅区的速度,并且我有三个文件。一个提供上下文SummaryContext,第二个是使用上下文的类组件WikiSummary,第三个是显示它Title。
但是,我收到以下错误,并且尽管我一直在搞乱(仍在学习),但我无法弄清楚为什么会收到此错误。
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `WikiSummary`.
摘要上下文
import React, { createContext } from 'react'
export const SummaryContext = createContext(null);
维基摘要
import React, {Component} from "react";
import {Title} from "./components/Title"
import {SummaryContext} from "../../contexts/SummaryContext"
import "../../App.css"
class WikiSummary extends Component {
render() {
return (
<>
<SummaryContext.Provider value = "hello from context">
<Title />
</SummaryContext.Provider>
</>
);
}
}
export default WikiSummary;
标题
import React, {useContext} from "react"
import {SummaryContext} from "../../../contexts/SummaryContext"
export function Title(){
const message = useContext(SummaryContext)
return(
<div>
<h2>{message}</h2>
</div>
)
}
沙箱显示与沙箱中不同的错误
https://codesandbox.io/s/react-context-example-forked-rly0d?file=/src/components/Title.js
守着一只汪
开心每一天1111
相关分类