How to import useSWR in PostLayout? #385
-
I try to import useSWR in PostLayout but get the following error import useSWR from "swr";
import Link from "@/components/Link";
import PageTitle from "@/components/PageTitle";
import { BlogSEO } from "@/components/SEO";
import Image from "@/components/Image";
import Tag from "@/components/Tag";
import siteMetadata from "@/data/siteMetadata";
import Comments from "@/components/comments";
import ScrollTopAndComment from "@/components/ScrollTopAndComment";
import NewsletterForm from "@/components/NewsletterForm";
import TOCInline from "@/components/TOCInline";
import TOC from "@/components/TOC";
const editUrl = (fileName) =>
`${siteMetadata.siteRepo}/blob/main/data/blog/${fileName}`;
const twitterShare = (slug) =>
`https://twitter.com/intent/tweet?url=${encodeURIComponent(
`${siteMetadata.siteUrl}/blog/${slug}`
)}`;
const facebookShare = (slug) =>
`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(
`${siteMetadata.siteUrl}/blog/${slug}`
)}`;
const postDateTemplate = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
export default function PostLayout({
frontMatter,
authorDetails,
next,
prev,
children,
toc,
}) {
const { slug, fileName, date, title, tags, readingTime } = frontMatter;
return (
<>
...
</>
);
} 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, or you might have mixed up default and named imports. I seem to find that the error maybe occurs in MDXComponents /* eslint-disable react/display-name */
import { useMemo } from "react";
import { getMDXComponent } from "mdx-bundler/client";
import CustomLink from "./Link";
import Image from "./PostImage";
import TOCInline from "./TOCInline";
import Pre from "./Pre";
import { BlogNewsletterForm } from "./NewsletterForm";
export const MDXComponents = {
Image,
TOCInline,
a: CustomLink,
pre: Pre,
BlogNewsletterForm: BlogNewsletterForm,
wrapper: ({ components, layout, ...rest }) => {
const Layout = require(`../layouts/${layout}`).default;
return <Layout {...rest} />;
},
};
export const MDXLayoutRenderer = ({ layout, mdxSource, ...rest }) => {
const MDXLayout = useMemo(() => getMDXComponent(mdxSource), [mdxSource]);
return <MDXLayout layout={layout} components={MDXComponents} {...rest} />;
}; I have another question to ask why the author doesn't start a group such as Facebook, Telegram, Discord. Or already have it? I think that will be convenient. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Have a look at this answer. Maybe it helps fixing your bug.
There are already GitHub discussions, which nicely work together with the hosting of the source code. What would be an advantage of the other systems compared to GitHub Discussions? |
Beta Was this translation helpful? Give feedback.
-
As to why not another group, I just like all discussions to be public and it's easier for people to search for information as well. I also don't have the capacity to support real-time chat. You could try the next.js discord group though. Most of the technical support questions tend to be around nextjs, tailwindcss or mdx and are not really specific to the template😃 |
Beta Was this translation helpful? Give feedback.
-
@timlrx @AlexanderZeilmann Thanks for solving my problem ❤️ |
Beta Was this translation helpful? Give feedback.
-
@tszhong0411, I checked Lee rob's blog and realized he is using swr fixed at version |
Beta Was this translation helpful? Give feedback.
Have a look at this answer. Maybe it helps fixing your bug.
There are already GitHub discussions, which nicely work together with the hosting of the source code. What would be an advantage of the other systems compared to GitHub Discussions?