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.

Mapped data passing same value to gatsby-link prop

See original GitHub issue

Hey all, first time post here i hope it’s allowed/i’m doing it right. Anyways I’m mapping over some data (3 image slides i have stored in a headless cms). I am passing the returned data into slide component, and from there its being passed down to a button component, which is just a ‘gatsby-link’. The mapped data contains a “page_link”, among other things that should be different for each slide. However, for some reason, the same “page_link” is being passed down to each button, even though they should all be different. I am logging my data in the console and can confirm that each array i am mapping over contains different data. I get the different image, title, text, etc fields for each slide, but i am receiving the same “page_link” for each slide. I am confused as to what i am doing wrong.

Heres my code:

GraphQL Query:

const HOMEPAGE_DATA = graphql`
  query {
    prismicHomePage {
      data {
        body {
        slider {
          title {
            text
          }
          text {
            text
          }
          page_link{
           text
          }
          button_text{
           text
          }
          image {
            localFile {
              childImageSharp {
                fluid(maxWidth: 1280, quality: 90) {
                  ...GatsbyImageSharpFluid_withWebp
                }
              }
            }
          }
        }
      }
    }
  }
`

  // get homepage data
  const data = useStaticQuery(HOMEPAGE_DATA)

  // simplify data
  const home_data = data.prismicHomePage.data

Mapped Data:

        {home_data.slider.map((slide, index) => {
          console.log(slide)
          return (
            <Slide
              key={slide.title.text}
              title={slide.title.text}
              image={slide.image.localFile.childImageSharp.fluid}
              text={slide.text.text}
              button_text={slide.button_text.text}
              path_link={slide.page_link.text}
            />
          )
        })}

Slide Component:

const Slide = props => {
  return (
    <StyledBackgroundImage fluid={props.image} alt={`${props.title}`}>
      <Wrapper>
        <SlideContent>
          <h1>{props.title}</h1>
          <p>{props.text}</p>

          <div className="slide-btn">
            <Button path={props.path_link} text={props.button_text} />
          </div>
        </SlideContent>
      </Wrapper>
    </StyledBackgroundImage>
  )
}

Button Component: The <StyledButton /> is just a Styled “Link” Component from Gatsby.

const Button = props => {
  return (
    <StyledButton to={props.path} title={`${props.text}`}>
      {props.text}
    </StyledButton>
  )
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
jonniebigodescommented, Jan 22, 2020

@vinyltek if you don’t mind creating a reproduction with your code and package you’re using, following these steps, i wouldn’t mind taking a look at it and possibly solve the issue you’re experiencing. Sounds good?

1reaction
jonniebigodescommented, Jan 25, 2020

@vinyltek that was my thought aswell, this small thing was the issue? To be honest, when i saw the issue i’ve linked,i was laughing like a mad man as at that stage i had amost of the website’s code rewritten… and your answer quasi ready to be posted 😃.

No need to thank, i’m glad that i was able to solve your issue.

If you don’t mind, i’m going to close this issue as it’s resolved, should any related items pop up feel free to re-open it and comment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Mapping Data Returning Same Value - Gatsby - Stack ...
I'm mapping over some data (3 image slides i have stored in a headless cms). I am passing the retuned data into slide...
Read more >
Passing data between pages in GatsbyJS - Rodrigo Pinto
This is the most straight-forward way to transfer data between pages in Gatsby. ... the same time, and transfer of data can be...
Read more >
Rendering nav links with .map() in React. - DEV Community ‍ ‍
React utilises .map() to render items in a list, this can be great for dynamically ... we can now move on to the...
Read more >
Compound Link Destination Using Gatsby Link Via Props ...
[Solved]-Compound Link Destination Using Gatsby Link Via Props-Reactjs ... Difficulty grabbing values of list items in mapped API data using React ...
Read more >
Customizing the GraphQL Schema - Gatsby
One of Gatsby's main strengths is the ability to query data from a variety of sources in a ... Setting default field values;...
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