question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content gets rendered twice, outside gatsby root only in production builds

See original GitHub issue

Description

Not too long ago I decided to move my React landing page to Gatsby - now I’ve hit a wall and I have no idea why my content gets rendered twice. Maybe this is expected behavoir, but I can’t find anything relatable in your docs.

I have a React FC that uses another FC that takes in some props - as soon as I add that component to the main page, the rest of the page gets rendered twice. What really bugs me is that the doubled content gets rendered outside the gatsby source root - feel free to have look, I have no idea how to fix it yet.

I get that gatsby uses GraphQL to load external data into a site, but I don’t want to load anything external, I’m just trying to pass some props between react components.

here’s the problematic code, if I remove it from the side everything works as expected

import { List, ListItem, ListItemIcon, ListItemText, makeStyles, Typography } from "@material-ui/core";
import React from "react"
import listItemChecked from "../../images/icons/list-item-checked.svg"
import listItemEmpty from "../../images/icons/list-item-empty.svg"

const useStyles = makeStyles((theme) => ({
    noMargin: {
        margin: 0
    }
}));

const PricingListItem = (text) => {
    let classes = useStyles()

    return (
        <ListItem>
            <ListItemIcon>
                <img alt="checked" src={listItemChecked}></img>
            </ListItemIcon>
            <ListItemText className={classes.noMargin}>
                <Typography component="li" variant="subtitle2">
                    {text}
                </Typography>
            </ListItemText>
        </ListItem>
    )
}

const PricingListItemMissing = (text) => {
    let classes = useStyles()

    return (
        <ListItem>
            <ListItemIcon>
                <img alt="checked" src={listItemEmpty}></img>
            </ListItemIcon>
            <ListItemText className={classes.noMargin}>
                <Typography component="li" variant="subtitle2" color="textSecondary">
                    {text}
                </Typography>
            </ListItemText>
        </ListItem>
    )
}


export const BasicPricingList = () =>{
    return (
        <List dense aria-label="pricing-basic">
        {PricingListItem("Basic influencer search")}
        {PricingListItem("Bookmarks")}
        {PricingListItem("Engagement rate")}
        {PricingListItem("Profile analytics")}
        {PricingListItem("Category filtering")}
        {PricingListItem("Follower filtering")}
        {PricingListItemMissing("Location filtering")}
        {PricingListItemMissing("Language filtering")}
        {PricingListItemMissing("Basic support")}
        {PricingListItemMissing("Report generation & export")}
        {PricingListItemMissing("Follower verification")}
        {PricingListItemMissing("Multiple users")}
        {PricingListItemMissing("24/7 support")}
    </List>
    )
}

Steps to reproduce

Not sure yet, I don’t know why other FC’s are not triggering this behaviour

Expected result

develop and build should result in the same behaviour

Actual result

when I run gatsby develop, everything renders correctly, but on gatsby build I get my components rendered a 2nd time outside the gatsby source root. Also the rest of the UI since the call to React FC is repeated too.

Environment

System: OS: macOS 11.0.1 CPU: (8) x64 VirtualApple @ 2.50GHz processor Shell: 5.8 - /bin/zsh Binaries: Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm Languages: Python: 2.7.16 - /usr/bin/python Browsers: Firefox: 85.0 Safari: 14.0.1 npmPackages: gatsby: ^2.26.1 => 2.30.2 gatsby-image: ^2.9.0 => 2.9.0 gatsby-plugin-google-analytics: ^2.9.0 => 2.9.0 gatsby-plugin-google-gtag: ^2.7.0 => 2.7.0 gatsby-plugin-manifest: ^2.10.0 => 2.10.0 gatsby-plugin-material-ui: ^2.1.10 => 2.1.10 gatsby-plugin-netlify-cms: ^4.8.0 => 4.8.0 gatsby-plugin-preact: ^4.5.0 => 4.5.0 gatsby-plugin-sass: ^3.0.0 => 3.0.0 gatsby-plugin-sharp: ^2.12.1 => 2.12.1 gatsby-plugin-sitemap: ^2.10.0 => 2.10.0 gatsby-source-filesystem: ^2.9.0 => 2.9.0 gatsby-theme-material-ui: ^1.0.13 => 1.0.13 gatsby-transformer-sharp: ^2.10.1 => 2.10.1 npmGlobalPackages: gatsby-cli: 2.18.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lmyslinskicommented, Feb 3, 2021

I’ve solved it. It’s really annoying dumb bug:

        <ListItem>
            <ListItemIcon>
                <img alt="checked" src={listItemChecked}></img>
            </ListItemIcon>
            <ListItemText className={classes.noMargin}>
                <Typography component="li" variant="subtitle2">
                    {text}
                </Typography>
            </ListItemText>
        </ListItem>

After some fiddling I finally got the console to report an error - I was rendering a <li> component under another <li> component. This (I don’t really know why) results in DOM getting rendered twice, but once I removed that everything works fine

0reactions
fullstackskcommented, Aug 29, 2021

const StyledIconButton = styled(IconButton) width:56px; height:56px;;

This was my mistake. I was trying to style MUi IconButton Component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gastby Homepage Components Double Rendering on all ...
Everything works fine in Development / Local but when the site gets deployed to Netlify the homepage components below the navbar are getting...
Read more >
The Perils of Rehydration - Josh W Comeau
This deep-dive tutorial examines how React and Gatsby can be used to pre-render content, and how we can work around the constraints to...
Read more >
React Components rendered twice — any way to fix this?
It only happens in development mode and should help to find accidental side effects in the render phase. Let's find out if there...
Read more >
Rendering Options - Gatsby
A rendering option defines the stage at which your page's user-facing HTML is generated. It can happen at build time (SSG or pre-rendering),...
Read more >
Frequently Asked Questions - Material UI - MUI
How can I access the DOM element? All MUI components that should render something in the DOM forward their ref to the underlying...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found