0

I've a react code as shown below in which I want to build a Timezonepicker. On clicking particular timezone, it should display all list related to it.  

React Code:

const timezones = [moment.tz("America/Toronto").format('z'), 'PT', 'MT', 'CT', 'ET', 'AT', 'NT'];

const Helloworld = (props) => {
    return (
        timezones.map((timezone) => {
            <a onClick = {
                (e) => {
                    e.preventDefault();
                    props.onChange(timezone)
                }
            } > timezone < /a>
        })
    )
}

return(
    <VersionPageHolder>
        {
            <Helloworld onChange={(timezone) => {
                setSelectedTimezone(timezone);
            }}
            />
        }
        <div>{content.title}</div>
        <div>Version List : </div>
        <div>{
            renderElements(selectedTimezone)
        }</div>
    </VersionPageHolder>
)

The react code above doesn't show list of timezones (PT, MT, CT, ET, AT, NT). With the code above. At this moment, I am currently seeing:

timezone timezone timezone timezone timezone timezone

2
  • 1
    Add {brackets} around "timezone" in your Helloworld return, before the closing tag Commented Mar 18, 2021 at 18:56
  • @gui3 This works. Commented Mar 18, 2021 at 18:59

4 Answers 4

1

To inject data inside jsx you should wrap them inside {}. Look closely to your map and repair it because you type only timezone without {}.

timezones.map((timezone) => 
    <a onClick = { (e) => { e.preventDefault(); props.onChange(timezone) } } > 
        { timezone } 
    </a>
)
Sign up to request clarification or add additional context in comments.

2 Comments

I copied the code but nothing is getting displayed. Am I missing anything ?
@flash There was also another issue with brackets after arrow. I changed the code, try again :)
0

To inject a variable in JSX, you'll need use {}.

Comments

0

Two issues. One, as pointed out by others, you need curly brackets to use JS in JSX - {}. And two, your map function needs to return the a tag, which it currently does not. You can fix that by using the return statement or just dropping the curly brackets inside the map:

return (
  <>
    {
      timezones.map((timezone) => 
        <a onClick = {
            (e) => {
                e.preventDefault();
                props.onChange(timezone)
            }
        } > timezone </a>
    )
  }
  </> 
)

Comments

0

Two issues one related to React and one related to Javascript.

  • First issue related aboud React: You should wrap the variables with curly braces {} to display the data, you can check this doc for more information. In your code it would be as follows:

} > {timezone} < /a>

  • Second issue related aboud Javascript: When you use Array.prototype.map() always try to return something. In your code it would be as follows with only parenthesis () instead of {}:
    timezones.map((timezone) => (
        <a onClick = {
            (e) => {
                e.preventDefault();
                props.onChange(timezone)
            }
        } > {timezone} < /a>
    ))

or with curly braces {} and return:

    timezones.map((timezone) => {
        return <a onClick = {
            (e) => {
                e.preventDefault();
                props.onChange(timezone)
            }
        } > {timezone} < /a>
    })

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.