Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
jsx-control-statements-jstransform
Advanced tools
Neater control statements (if/for) for jsx, running in jstransform
NOTE: This is the JSTransform version of https://github.com/AlexGilleran/jsx-control-statements This version has been split off because JSTransform is no longer actively maintained and having to do everything twice is slowing down development of jsx-control-statements - hence this version is no longer maintained. This repo is available in case there's still people who use the JSTransform version and want to develop it or submit pull requests.
React and JSX are great, but to those of us who are used to dedicated templating libraries like Handlebars, the control
statements (e.g. if conditions and for loops) are a step backwards in terms of neatness and readability. What's worse is
that JSX is perfectly capable of using the kind of conditional and looping logic we're used to, but it has to be done
through ugly use of ternary ifs and Array.map
, or by doing this logic in javascript before you start defining your
actual view, which in my mind turns it into spaghetti.
Wouldn't it be easier if we could just have some syntactical sugar that turned neat <If>
/<Else />
/</If>
and
<For>
/</For>
tags into ternary ifs and Array.map
, so you could read your render functions a bit more easily?
So that's what this does. It's a set of JSTransform visitors that run just before JSX transpilation and perform
desugaring from<If>
-> ? :
and <For>
-> Array.map
.
Define an <If>
tag like so:
<If condition={this.props.condition === 'blah'}>
<span>IfBlock</span>
<Else />
<span>ElseBlock</span>
</If>
or
<If condition={this.props.condition === 'blah'}>
<span>IfBlock</span>
</If>
This will desugar into:
this.props.condition === 'blah' ? (
<span>IfBlock</span>
) : (
<span>ElseBlock</span>
)
or
this.props.condition === 'blah' ? (
<span>IfBlock</span>
) : ''
<If>
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 <If>
block can only contain a single
tag, for instance.
Define <For>
like so:
<For each="blah" index="index" of={this.props.blahs}>
<span key={blah}>{blah + this.somethingElse} at {index}</span>
</For>
and this will desugar into:
this.props.blahs.map(function(blah, index) { return (
<span key={blah}>{blah + this.somethingElse} at {index}</span>
)}, this)
The <For>
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 <For>
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 <If>
, 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:
<For each="blahKey" of={Object.keys(this.props.blahObj)}>
<span key={blahKey}>{blahObj[blahKey]}</span>
</For>
See here.
FAQs
Neater control statements (if/for) for jsx, running in jstransform
The npm package jsx-control-statements-jstransform receives a total of 2 weekly downloads. As such, jsx-control-statements-jstransform popularity was classified as not popular.
We found that jsx-control-statements-jstransform demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.