Exporting components as the default from Next.js leads to a forced page refresh whenever there is a change

One issue I encountered involved having an index.tsx file containing all the shared UI components. When I exported the Footer and imported it into my shared Layout.tsx, which is utilized across all pages, the browser would refresh the page every time a change was made rather than performing a fast refresh.

This behavior occurred whenever I tried to default export any component and include it wherever needed.

Surprisingly, all child components functioned perfectly fine. The issues only arose when default exporting everything.

I am curious if there are any solutions, fixes, or reasons for this problem?

/components/ui/index.tsx:

export { default as Alert } from "./Alert"
export { default as Footer } from "./footer"
export { default as Logo } from "./logo"
export { default as MainNavigation } from "./main-navigation"
export { default as MaxWidthWrapper } from "./max-width-wrapper"
export { default as PageAnimate } from "./page-animate"
export { default as ThemeSwitch } from "./theme-switch"

Layout.tsx:

"use client"

import { MaxWidthWrapper, PageAnimate } from "@components/ui"

import { Footer, MainNavigation } from "@components/ui"

const Layout = ({ children }: { children: React.ReactNode }) => {
  return (
    <>
      <MainNavigation />
      <PageAnimate>
        <MaxWidthWrapper type="main">{children}</MaxWidthWrapper>
      </PageAnimate>
      <Footer />
    </>
  )
}

export default Layout

Footer.tsx:

"use client"

import { FaFacebook, FaTwitter, FaInstagram, FaYoutube } from "react-icons/fa"

import MaxWidthWrapper from "./max-width-wrapper"

import { SocialProps } from "types"

const Footer = ({ socialFields }: SocialProps) => {
  return (
    <MaxWidthWrapper
      container
      type="footer"
      className="flex flex-row items-center justify-between py-5 flex-nowrap"
    >
      <p className="text-xs">
        &copy; {new Date().getFullYear()} The Friendly Vegan24
      </p>

      <div className="flex justify-center space-x-3">
        <a
          target="_blank"
          rel="noreferrer"
          href="https://www.facebook.com"
          title="Instagram"
          className="flex items-center justify-center text-white rounded-full w-7 h-7 bg-sun sm:w-8 sm:h-8"
        >
          <FaFacebook className="w-4 h-4 text-midnight" />
        </a>

        <a
          target="_blank"
          rel="noreferrer"
          href="https://www.instagram.com"
          title="Facebook"
          className="flex items-center justify-center text-white rounded-full w-7 h-7 bg-sun sm:w-8 sm:h-8"
        >
          <FaInstagram className="w-4 h-4 text-midnight" />
        </a>

        <a
          target="_blank"
          rel="noreferrer"
          href="https://twitter.com"
          title="Twitter"
          className="flex items-center justify-center text-white rounded-full w-7 h-7 bg-sun sm:w-8 sm:h-8"
        >
          <FaTwitter className="w-4 h-4 text-midnight" />
        </a>

        <a
          target="_blank"
          rel="noreferrer"
          href="https://youtube.com"
          title="Twitter"
          className="flex items-center justify-center text-white rounded-full w-7 h-7 bg-sun sm:w-8 sm:h-8"
        >
          <FaYoutube className="w-4 h-4 text-midnight" />
        </a>
      </div>
    </MaxWidthWrapper>
  )
}

export default Footer

/pages/index.tsx (home)

import { NextSeo } from "next-seo"

import { ButtonProps } from "types"

import { Layout, SectionHeader } from "@components/sections"
import {
  Hero,
  InfoCard,
  Feature,
  Newsletter,
  BlogCard,
  Contact
} from "@components/blocks"
import { Grid, Section } from "@components/shared"

import HeroImage from "@images/hero.png"

const HomePage = () => {
  const HeroButtons: ButtonProps[] = [
    {
      id: 1,
      href: "/",
      text: "Get in touch!"
    },
    {
      id: 2,
      href: "/over-ons",
      text: "Over ons"
    }
  ]

  return (
    <Layout>
      <NextSeo
        title="Home"
        openGraph={{
          type: "website",
          url: "https://ffb-next-static.vercel.app",
          description:
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec suscipit diam, ac scelerisque libero. Vestibulum sapien lorem, placerat quis orci id, tempor mattis felis.",
          images: [
            {
              url: "https://images.pexels.com/photos/1059823/pexels-photo-1059823.jpeg?auto=compress&cs=tinysrgb&w=1200&h=630&dpr=2",
              width: 1200,
              height: 630,
              alt: "We welcome you",
              type: "image/jpeg"
            }
          ]
        }}
      />

      <Hero
        funny
        buttons={HeroButtons}
        featuredImage={HeroImage.src}
        title="The Friendly Vegan"
        intro="Ruimte voor tekst om de bezoeker welkom te heten, of om te
            beschrijven wat je doet met je product of dienst."
      />

      <Section>
        <SectionHeader
          title="Onze werkzaamheden"
          text="Ruimte voor tekst om in te leiden welke stappen je hieronder gaat uitleggen. Bijvoorbeeld de waarden waar je bedrijf voor staat, of het proces wat je doorloopt."
        />

        <Grid>
          <InfoCard
            featuredImage="https://source.unsplash.com/1024x800/?food?1"
            title="Recepten"
            text="Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quas natus commodi repellendus."
          />

          <InfoCard
            featuredImage="https://source.unsplash.com/1024x800/?food?2"
            title="Catering"
            text="Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quas natus commodi repellendus."
          />

          <InfoCard
            featuredImage="https://source.unsplash.com/1024x800/?food?3"
            title="Workshops"
            text="Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quas natus commodi repellendus."
          />

          <InfoCard
            featuredImage="https://source.unsplash.com/1024x800/?food?4"
            title="Duurzaamheid"
            text="Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quas natus commodi repellendus."
          />
        </Grid>
      </Section>

      <Section>
        <Feature />
      </Section>

      <Section className="bg-sun sm:bg-transparent">
        <Newsletter />
      </Section>

      <Section>
        <SectionHeader
          buttonLink="/blog"
          buttonText="Naar alle artikelen"
          title="Blog"
          text="Ruimte voor tekst om in te leiden welke stappen je hieronder gaat uitleggen. Bijvoorbeeld de waarden waar je bedrijf voor staat, of het proces wat je doorloopt."
        />

        <Grid className="grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 pb-14">
          <BlogCard image="https://source.unsplash.com/1024x800/?food?8" />
          <BlogCard className="text-midnight bg-primary" />
          <BlogCard className="text-midnight bg-neutral" />
        </Grid>
      </Section>

      <Section className="bg-sun sm:bg-transparent">
        <Contact />
      </Section>
    </Layout>
  )
}

export default HomePage

Answer №1

Avoid using default exports whenever possible:

Footer.tsx:

export const FooterComponent = () => {
 //...
}

index.tsx:

export * from "./footer"
//...

Layout.tsx:

import { FooterComponent, MainNavigation } from "@components/ui"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Data structure designed specifically for a drawing tool application

Currently, I am in the process of developing a unique sketching application using the HTML5 Canvas element. Despite my progress, there is one particular challenge that has stumped me. The main concept of the application involves allowing users to manipula ...

The functionality of the delete button in Material UI Chip seems to be malfunctioning

I am working on mapping Material UI Chips, but I am facing an issue where the cross button is not showing up and I cannot click on them or use the onTouchTap event. Below is my mapping: {mapping.map(chipMap => { return ( <div> ...

Adjust rankings based on the number of upvotes received by a project

I'm facing a challenge with ranking projects based on the number of votes they receive. No matter the vote count, the project always ends up getting ranked as 1. To address this issue, I developed a function to manage the rank count and a return hand ...

execute a function for every regex match found within a string

When working with WordPress or PHP in general, I came across this interesting recommendation for email obfuscation, and I would like to: Convert email addresses to mailto links Encode the mailto links using str_13() Decode them client-side using JavaScri ...

Avoid cascading of the 'display' property in JavaScript styling to prevent inheritance

Is there a way in Javascript to toggle the visibility of a larger portion of HTML without affecting inner display properties with display: <value>? Despite setting an outer display property using Javascript, the inner display properties are also alt ...

What is the best way to make a trail follow my shapes on canvas?

In my current project, I have implemented a feature where shapes are generated by spinning the mouse wheel. One specific shape in focus is the triangle, but other shapes follow the same generation process. The goal is to create a trail that slowly fades ...

Removing entries in a Google spreadsheet by utilizing the API and executing a BatchUpdateRequest

Currently, I am working on sending a BatchUpdateRequest to the Google Sheets API in order to delete a row of data from a spreadsheet. Here is how my request is structured: var spreadsheetId = '1EV8S8AaAmxF3vP0F6RWxKIUlvF6uFEmsrOFWA1oNBYI'; ...

Enhance your slideshows with React-slick: Integrate captivating animations

I recently built a slider using react slick, and now there is a need to adjust the transition and animation of slides when the previous and next buttons are clicked. I received some advice to add a class to the currently active slide while changing slide ...

The image failed to load in React/Express

I'm currently working on a React/Express app and I've encountered an issue with images not loading. Instead, I see the message "could not load the image." The CSS styles are loading fine, but the images are not showing up. I suspect there might ...

It is impossible to alter the data contained within the input box

Objective: Upon clicking a button to display the modal, the selected data (first and last name) should be inserted into an input box and editable. The user should be able to modify the data. Issue: The data entered into the input box cannot be edited ...

Tips for ensuring a custom menu closes when clicking outside of it instead of on the menu itself

Building off a recent inquiry, I am aiming to have my menu close whenever I click outside of it. At the moment, clicking the "Hamburger Menu Button" opens and closes the menu. It will also close when I click a link on the menu or the menu itself. However, ...

JavaScript makes it possible to access subnodes in XML by utilizing specific methods and

I am looking to utilize javascript to extract data from an XML file that has been loaded into a webpage. Below is the XML file (a.xml) that I am working with. a.xml <?xml version="1.0"?> <Step rID="T6"> <Obj ><![CDATA[Get Data Ta ...

Encountering TypeError with Next.js and Firebase: Unable to access properties of undefined (specifically 'apps')

My goal is to create an authentication system using NextJS and Firebase. The issue I am facing is in my firebaseClient.js file, where I am encountering the error "TypeError: Cannot read properties of undefined (reading 'apps')". Here is a snipp ...

What is the best way to iterate through an array containing multiple objects in order to create a graph?

My API response contains multiple objects organized in an array: [{"symbol":"AAPL","date":"2020-02-27","adj_close":68.24}, {"symbol":"TSLA","date":"2020-02-27","adj_close":133.8}, {"symbol":"TSLA","date":"2020-02-28","adj_close":122.3}, {"symbol":"AAPL" ...

Is it possible to achieve dynamic updating in Angular without utilizing ng-repeat? (with the use of Firebase)

Is there a way to dynamically update the DOM without utilizing ng-repeat in your template? It appears that when using ng-repeat to load a list of objects, any additions or deletions from the database automatically reflect in the DOM. However, if I simply u ...

Typescript error: Cannot access property "status" on type "never".ts(2339)

Currently, I have a method that utilizes nextjs/auth to sign in with credentials from a form. However, I am encountering a type checking error Object is possibly 'undefined'.ts(2532) const doStuff = async (values: any) => { const result: S ...

CSS transition fails to revert

My standard opacity animation is not working in reverse order. Here is a link to the JSFiddle example. According to the documentation, it should work automatically. Since I am new to JavaScript, I am unsure if this issue lies in my code or if the CSS anima ...

Error Type: Property 'history' is not defined and cannot be read

My goal is to automatically redirect the user to the login page when they hit the /logout route. The authentication process works as expected (jwt token is removed), but I'm encountering issues with the app failing to redirect to /login upon logout. ...

Recording a message from an HTML input using NodeJS and socket.io

I am currently working on a project where I want to log user input from an input box to the NodeJS console. For example, if a user types "Hello world" into the input box and clicks "Send message to console", I want it to show up as "Hello world" in the con ...

Creating a password with two distinct numbers using regular expressions

In javascript I am struggling to create a password that meets the criteria of having at least eight characters, including two SEPARATE digits, one uppercase and one lowercase letter, as well as one special character (-, @, #, $, &, *, +) but not /, !, ? ...