wix-incubator / react-templates

Light weight templates for react
https://wix.github.io/react-templates
MIT License
2.82k stars 206 forks source link

how to output text from expression? #149

Closed PierBover closed 8 years ago

PierBover commented 8 years ago

So I have a <span> that informs the user how many files there are in a folder.

I want to be able to tell if there is 1 file or 5 files (adding an s for the plural).

I have come up with this (which doesn't work) to output the final s if there are more than 1 file.

<span>{files.length} file{() => files.length > 1 return "s"}</span>

What would be the best way to handle this?

nippur72 commented 8 years ago

simply:

<span>{files.length} file{files.length > 1 ? 's' : ''}</span>
or
<span>{files.length} file{files.length > 1 ? 's' : null}</span>
or
<span>{files.length} {files.length > 1 ? 'files' : 'file'}</span>

Your example didn't work because:

  1. lambda expression function needs to be "called" : {(()=> l > 1 ? 's' : '')()}
  2. lambda expression needs also to be translated into ES5 (babel, etc).
PierBover commented 8 years ago

Thanks a lot, as you can see I'm still figuring out this react + templates + ES6 thing.