Using Template Literals In JSX That Also Returns HTML

This is the following code:

    <div>{
        !breakStarts ? 
        `Work:` {workMinutes}`:`{workSeconds < 10 ? `0${workSeconds} :` workSeconds}} : 
        `Break:` {breakMinutes}`:`{breakSeconds < 10 ? `0${breakSeconds} :` breakSeconds}}
        }
    </div>

As you can see, I want different messages to display depending on the truthiness of breakStarts. However, I can’t seem to use template literals in JSX. Is doing this possible or is there a better way?

Did you meant to do something like this?

const result = !breakStarts ? 
    (workSeconds < 10 ? `Work: ${workMinutes}:0${workSeconds}` : `Work: ${workMinutes}:${workSeconds}`):
    (breakSeconds < 10 ? `Break: ${breakMinutes}:0${breakSeconds}` : `Break: ${breakMinutes}:${breakSeconds}`)
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.