Reputation: 65
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)
Upvotes: 2
Views: 1309
Reputation: 67539
The actual problem is an import/export syntax issue. I'll walk you through how to find it.
The problem's real location is shown by the component stack trace:
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
If we look in TopNews.js
, it only renders <h3>
, <div>
, and <NewsItem>
. h3
and div
are just basic tags, so it's probably NewsItem
that's the problem.
At the top of TopNews.js
, you have:
import { NewsItem } from '../components/NewsItem';
However, in NewsItem.js
, you have:
export default NewsItem ;
So, you're doing a default export, but a named import, and so NewsItem
is undefined in TopNews.js
.
Try changing it to be import NewsItem from "../components/NewsItem"
and it should work.
Upvotes: 1