0

I am using Next.js and mapping out some components with the below code. All works as it should but I would really like to destrcuture the object properties here to not be repeating the paths inside my props

 {sortedData.map((service) => (
          <Article
            key={uuidv4()}
            title={service.fields.title}
            image={service.fields.thumbnail.fields.file.url}
            alt={service.fields.thumbnail.fields.file.fileName}
            slug={service.fields.slug}
            content={service.fields.intro}
            height={service.fields.thumbnail.fields.file.details.image.height}
            width={service.fields.thumbnail.fields.file.details.image.width}
          />
        ))}

I tried looking for similar questions here but could not find any. Any help is appreciated

2
  • 3
    .map(({ fields: { title, ... } }) =>? Commented Jan 4, 2021 at 16:07
  • If you don't know how to do destructuring, use the standard approach and introduce temporary variables: service => { const fields = service.fields; const thumbFile = fields.thumbnail.fields.file; return …; } Commented Jan 4, 2021 at 17:05

1 Answer 1

2

Desestructure service object, example:

   {sortedData.map(({ fields: { title, thumbnail, slug, intro } }) => (
      <Article
        key={uuidv4()}
        title={title}
        image={thumbnail.fields.file.url}
        alt={thumbnail.fields.file.fileName}
        slug={slug}
        content={intro}
        height={thumbnail.fields.file.details.image.height}
        width={thumbnail.fields.file.details.image.width}
      />
    ))}
Sign up to request clarification or add additional context in comments.

Comments

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.