Neater control statements (if/for) for jsx, running in jstransform
npm install jsx-control-statements-jstransformArray.map, or by doing this logic in javascript before you start defining your
// and
/ tags into ternary ifs and Array.map, so you could read your render functions a bit more easily?
-> ? : and -> Array.map.
tag like so:
IfBlock
ElseBlock
`
or
`
IfBlock
`
This will desugar into:
`
this.props.condition === 'blah' ? (
IfBlock
) : (
ElseBlock
)
`
or
`
this.props.condition === 'blah' ? (
IfBlock
) : ''
`
tags must have a condition attribute which is expected to be some kind of expression (i.e. contained within
{}. All the normal rules for putting JSX tags inside ternary ifs apply - the block can only contain a single
tag, for instance.
For Tag
Define like so:
`
{blah + this.somethingElse} at {index}
`
and this will desugar into:
`
this.props.blahs.map(function(blah, index) { return (
{blah + this.somethingElse} at {index}
)}, this)
`
The tag expects an each attribute as a string (with "" around it) - this is what you'll reference for each
item in the array - and an of attribute which is an expression (with {} around it) that refers to the array that
you'll loop through. You can also include an index attribute which will resolve to the index of the current item in
the array, but it's optional.
Note that a cannot be at the root of a render() function in a React component, because then you'd
potentially have multiple components without a parent to group them which isn't allowed. As with , the same rules
as using Array.map() apply - each element inside the loop should have a key attribute that uniquely identifies it.
To loop across an Object, use Object.keys() like so:
`
{blahObj[blahKey]}
``