Using Stack Traces in React JS, Gatsby to Locate Errors

The client is facing an error in their project, which uses React JS, Gatsby, and Material UI. The main issue reported involves list rendering in React, specifically the need for unique “key” props for children in a list. The client mentions that they have manually checked all .map() and .forEach() loops in their code to ensure keys are properly added, but they find this approach inefficient and impractical.

Additionally, they face difficulty using the stack trace to locate the exact source of the error, as the hyperlinks in the error log take them to internal parts of React, Gatsby, or Material UI, rather than their own code. The client is still relatively new to front-end development and is looking for guidance on how to read and use stack traces effectively to find the specific lines in their code causing this error.

Error Type:

  • The error is a React warning: “Each child in a list should have a unique ‘key’ prop.”
  • This warning typically appears when rendering lists in React using .map() or .forEach() without assigning unique keys to the elements.

I am looking to find the line in the code that generated this error:

Error Script

codereact-dom.development.js:67 Warning: Each child in a list should have a unique "key" prop. See https://reactjs.org/link/warning-keys for more information.
at div
at Grid (webpack-internal:///./node_modules/@material-ui/core/esm/Grid/Grid.js:235:35)
at WithStyles (webpack-internal:///./node_modules/@material-ui/styles/esm/withStyles/withStyles.js:61:31)
at div
at Grid (webpack-internal:///./node_modules/@material-ui/core/esm/Grid/Grid.js:235:35)
at WithStyles (webpack-internal:///./node_modules/@material-ui/styles/esm/withStyles/withStyles.js:61:31)
at div
at Grid (webpack-internal:///./node_modules/@material-ui/core/esm/Grid/Grid.js:235:35)
at WithStyles (webpack-internal:///./node_modules/@material-ui/styles/esm/withStyles/withStyles.js:61:31)
at div
at div
at Container (webpack-internal:///./node_modules/@material-ui/core/esm/Container/Container.js:84:23)
at WithStyles (webpack-internal:///./node_modules/@material-ui/styles/esm/withStyles/withStyles.js:61:31)
at ContentWrapper (webpack-internal:///./src/components/content-wrapper.js:26:23)
at div
at Grid (webpack-internal:///./node_modules/@material-ui/core/esm/Grid/Grid.js:235:35)
at WithStyles (webpack-internal:///./node_modules/@material-ui/styles/esm/withStyles/withStyles.js:61:31)
at Footer (webpack-internal:///./src/components/common/footer/index.js:127:28)
at Layout (webpack-internal:///./src/components/Layout/index.js:28:23)
at LandingTemplate (webpack-internal:///./src/templates/landing/index.js:105:23)
at PageRenderer (webpack-internal:///./.cache/page-renderer.js:23:29)
at PageQueryStore (webpack-internal:///./.cache/query-result-store.js:39:30)
at RouteHandler
at div
at FocusHandlerImpl (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:359:5)
at FocusHandler (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:330:19)
at RouterImpl (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:235:5)
at Location (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:64:23)
at Router
at ScrollHandler (webpack-internal:///./node_modules/gatsby-react-router-scroll/scroll-handler.js:36:35)
at RouteUpdates (webpack-internal:///./.cache/navigation.js:286:32)
at EnsureResources (webpack-internal:///./.cache/ensure-resources.js:22:30)
at LocationHandler (webpack-internal:///./.cache/root.js:67:29)
at Location (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:64:23)
at Root
at ThemeProvider (webpack-internal:///./node_modules/@material-ui/styles/esm/ThemeProvider/ThemeProvider.js:41:24)
at TopLayout (webpack-internal:///./plugins/gatsby-plugin-top-layout/TopLayout.js:22:23)
at StylesProvider (webpack-internal:///./node_modules/@material-ui/styles/esm/StylesProvider/StylesProvider.js:49:24)
at GatedContentHandler (webpack-internal:///./src/utils/GatedContentHandler.js:31:23)
at LocationProvider (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:84:5)
at Location (webpack-internal:///./node_modules/@gatsbyjs/reach-router/es/index.js:64:23)
at withLocation (webpack-internal:///./src/utils/GatedContentHandler.js:70:24)
at eval (webpack-internal:///./src/components/Search/SearchContext.js:46:25)
at SalesforceProvider (webpack-internal:///./src/components/Salesforce/SalesforceProvider.js:28:66)
at CookiesProvider (webpack-internal:///./node_modules/react-cookie/es6/CookiesProvider.js:24:28)
at AuthProvider (webpack-internal:///./src/utils/context/AuthContext.js:50:23)
at ApolloProvider (webpack-internal:///./node_modules/@apollo/client/react/context/ApolloProvider.js:12:21)
at Apollo (webpack-internal:///./src/providers/apollo.js:34:23)
at StaticQueryStore (webpack-internal:///./.cache/query-result-store.js:127:32)
at ErrorBoundary (webpack-internal:///./.cache/fast-refresh-overlay/components/error-boundary.js:24:35)
at DevOverlay (webpack-internal:///./.cache/fast-refresh-overlay/index.js:114:23)
at RootWrappedWithOverlayAndProvider
at App (webpack-internal:///./.cache/app.js:209:68)

    The warning you’re encountering is related to missing or non-unique key props for elements inside a list. In React, whenever you map over an array to render a list of elements, each child must have a unique key prop. This helps React efficiently update and render components.

    Here’s how you can resolve the issue based on the stack trace:

    Problem:

    You are using Material-UI’s Grid component, and within your JSX structure, you are rendering a list of items without providing a key prop for each child.

    Solution:

    You need to identify where the list is being rendered (e.g., using .map() or similar methods) and add a unique key prop to each item.

    Here’s an example of how to fix it:

    Before (with missing key):

    code<Grid container>
    {items.map((item) => (
    <Grid item>{item.name}</Grid> // Missing `key` prop
    ))}
    </Grid>

    After (with correct key):

    code<Grid container>
    {items.map((item) => (
    <Grid item key={item.id}>{item.name}</Grid> // Added unique `key` prop
    ))}
    </Grid>

    In this case:

    • If your item objects have a unique id property, you can use key={item.id}.
    • If id isn’t available, ensure you use another unique attribute, or as a last resort, use the array index (key={index}).

    General Approach:

    • Locate the List: Find where you are rendering a list using .map(), .forEach(), or similar iteration methods.
    • Add Unique Key: Add a unique key prop to each element rendered in the list. The value of the key should ideally be a unique identifier for that item (such as id), not just the array index unless necessary.

    Complete Example in React.js:

    codeimport React from 'react';
    import Grid from '@material-ui/core/Grid';

    const MyComponent = ({ items }) => {
    return (
    <Grid container>
    {items.map((item) => (
    <Grid item key={item.id}>
    {item.name}
    </Grid>
    ))}
    </Grid>
    );
    };

    export default MyComponent;

    In this example:

    • items is an array passed as a prop.
    • The key={item.id} ensures each Grid item has a unique key.

    By following this approach, the warning will be resolved, and React will be able to properly manage the list of elements.

    Related blog posts