0

I want to understand why the compiled version of this code is invalid

<Fragment>
  {listData.list.description && (
    <div>
      <div dangerouslySetInnerHTML={{ __html: list.description }} />
      {hasTopCollection && <ImageAttribution />}
    </div> 
       // ^ ) expected
    {list.listOptions.brandHeaderType === 20 && <SomeComponent />}
 // ^ Parsing error: Unexpected token, expected ","
  )}
</Fragment>

I had this listData.list.description && <Description> component in a parent component, but now I want to move this logic into the actual <Description> component.

So before this was like this:

<Fragment>
    <div>
      <div dangerouslySetInnerHTML={{ __html: list.description }} />
      {hasTopCollection && <ImageAttribution />}
    </div>
    {list.listOptions.brandHeaderType === 20 && <SomeComponent />}
</Fragment>
3
  • @DedaDev actually the fragment is used to group more than one component Commented Aug 6, 2020 at 18:54
  • you are right actually. Commented Aug 6, 2020 at 18:55
  • I think it is the double curly, you cant have curly inside curly, brackets behave as a new component. Commented Aug 6, 2020 at 18:57

1 Answer 1

1

The <Fragment> now only has one child, so it isn't really needed. However, inside the parentheses you have two things. And parentheses alone aren't sufficient to group children, so a <Fragment> can be used there in their place:

{listData.list.description &&
  <Fragment>
    <div>
      <div dangerouslySetInnerHTML={{ __html: list.description }} />
      {hasTopCollection && <ImageAttribution />}
    </div> 
    {list.listOptions.brandHeaderType === 20 && <SomeComponent />}
  </Fragment>}
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.