0

How can I combine in an array the data from const = menuGallery, menuCompany and menuTour to all be called within menuData (see code below) so that my li should display the graphQl data from all three in const = menuData

render() {
  const menuGallery = get(this.props, 'data.allContentfulImageGallery.edges')
  const menuCompany = get(this.props, 'data.allContentfulCompanyPage.edges')
  const menuTour = get(this.props, 'data.allContentfulTourPage.edges')
  const menuData = [{menuGallery},{menuCompany},{menuTour}]

  return(
    {menuData.map(({ node: page })=> (
      <li className={slug === page.slug ? "selected" : ""} key={page.id}><a href={`/${page.slug}`}>{page.title}</a></li>
  ))}

if I change the current const menuData to be const menuData = menuGallery it will show the data from the allContentfulImageGallery but when combining multiples they don't map the data.

the graphQl query

export const menuQuery = graphql`
query menuQuery($slug: String!) {
    allContentfulCompanyPage(sort: {fields: [menuOrder], order: ASC}) {
      edges {
        node {
          id
          title
          slug
        }
      }
    }
    allContentfulTourPage(sort: {fields: [menuOrder], order: ASC}) {
      edges {
        node {
          id
          title
          slug
        }
      }
    }
    allContentfulImageGallery(sort: {fields: [menuOrder], order: ASC}) {
      edges {
        node {
          id
          title
          slug
        }
      }
    }
  }
`

Each works and is mapped individually but I have multiple content types that make up the App pages and want to list them together.

1 Answer 1

5

Sounds like you want to use the spread operator to combine three arrays into a single long one.

const menuData = [...menuGallery, ...menuCompany, ...menuTour]
Sign up to request clarification or add additional context in comments.

2 Comments

Stellar! Thanks - are you able to help me understand what purpose the ... have? I'll accept this as the answer once it allows.
Glad to help! Spread is a great ES6 feature for both arrays and objects-- just remember, that it is doing shallow-copies, so it can't be used as a deep-cloning utility. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.