gpt4 book ai didi

html - 错误 : Element type is invalid: expected a string (for built-in components) or a class/function

转载 作者:行者123 更新时间:2023-11-28 14:46:16 24 4
gpt4 key购买 nike

https://stackblitz.com/edit/react-redux-realworld-j95tpu?file=actions/index.js

code
export function fetchPosts(channel) {
return function (dispatch) {
dispatch(requestPosts());
return fetch(`https://newsapi.org/v1/articles?source=${channel}&apiKey=${MY_API_KEY}`)
.then(response => response.json(),
error => console.log('An error occurred.', error),
)
.then((json) => {
dispatch(receivedPosts(json));
}, );
};
}

error

VM1672:37 Warning: React.createElement: 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. Check the render method of `TopNews`.
in TopNews (created by Connect(TopNews))
in Connect(TopNews) (created by App)
in div (created by App)
in App
in Provider


Uncaught (in promise) Error: 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. Check the render method of `TopNews`.
at invariant (invariant.js:42)
at instantiateReactComponent (instantiateReactComponent.js:74)
at instantiateChild (ReactChildReconciler.js:44)
at eval (ReactChildReconciler.js:71)
at traverseAllChildrenImpl (traverseAllChildren.js:77)
at traverseAllChildren (traverseAllChildren.js:172)
at Object.instantiateChildren (ReactChildReconciler.js:70)
at ReactDOMComponent._reconcilerInstantiateChildren (ReactMultiChild.js:185)
at ReactDOMComponent.mountChildren (ReactMultiChild.js:224)
at ReactDOMComponent._createInitialChildren (ReactDOMComponent.js:703)

最佳答案

实际问题是导入/导出语法问题。我将向您介绍如何找到它。

问题的真实位置由组件堆栈跟踪显示:

Warning: React.createElement: 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. Check the render method of `TopNews`.
in TopNews (created by Connect(TopNews))
in Connect(TopNews) (created by App)
in div (created by App)
in App
in Provider

如果我们查看 TopNews.js , 它只呈现 <h3> , <div> , 和 <NewsItem> . h3div只是基本标签,所以它可能是NewsItem这就是问题所在。

TopNews.js 的顶部,你有:

import { NewsItem } from '../components/NewsItem';

然而,在NewsItem.js ,你有:

export default NewsItem ;

因此,您进行的是默认导出,但进行的是命名导入,所以 NewsItemTopNews.js 中未定义.

尝试将其更改为 import NewsItem from "../components/NewsItem"它应该有效。

关于html - 错误 : Element type is invalid: expected a string (for built-in components) or a class/function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52284797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com