2

What is wrong with the following code? I have tried many options given in the link.

  { 
      !hideCurrentLocation && (
        {currentLocation?.city}
        {currentLocation?.city && ', '}
        {currentLocation?.country_code2}
      )
  }

It is giving error on line {currentLocation?.city} that a comma is required.

1 Answer 1

2

Too many brackets. Also, if you're trying to render a "city, countrycode" you need to return a single node for valid JSX. I suggest dumping the city and country code into an array and joining them into a single string.

{!hideCurrentLocation && (
  <>
    {[currentLocation?.city, currentLocation?.country_code2]
      .filter(Boolean)
      .join(", ")}
  </>
)}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, there is no error now. But it is not printing city and country code when !hideCurrentLocation == true. Both are strings but getting converted into boolean.
@UmairJameel Oops, meant to filter the array, sorry. the .filter(Boolean) will filter out falsey values, leaving only the defined city or country_code2 left to join together.

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.