简体   繁体   中英

Rendering Multiple jsx with map and contains throws syntax error : ReactJS

I am trying to render jsx using map and then using includes to display a text based on the value present in the list. It throws error saying "message": "Unexpected token. Did you mean {'}'} or }`. Can someone help me with this

The code that I have tried:

  return (
    <>
      {list.length
        ?
          list.map((item, idx) => {
            return (
              <span key={idx}>
                {item}
              </span>
            )}
          {list.includes("Test") && (
            <span key="test">
              Replacement
            </span> )
          }
        )
        : "-"}
    </>
  );

You are calling list.includes inside your map function after you closed the curly bracket. I think you wanted something like this:

  return (
    <>
      {list.length
        ?
          (<>
           {list.map((item, idx) => {
            return (
              <span key={idx}>
                {item}
              </span>
            )}
          )}
          {list.includes("Test") && (
            <span key="test">
              Replacement
            </span> )
          }
        </>)
        : "-"}
    </>
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM