
Product
Introducing Repository Labels and Security Policies
Socket is introducing a new way to organize repositories and apply repository-specific security policies.
eslint-plugin-jsx-falsy
Advanced tools
Avoid accidentally rendering falsy values in your JSX. Only works with @typescript-eslint/parser
, and uses type information.
Exposes a single eslint rule, no-falsy-and
, that errors if the left side of an inline &&
expression in JSX is a string or number. These expressions can cause unwanted values to render, and can even cause some crashes in React Native, when the string or number is falsy (""
or 0
).
function MyComponent(props: {
str: string;
num: number;
maybeString: string | null;
maybeObj: {} | null;
}) {
return (
<div>
{props.str && <ComponentX /> /* error */}
{!!props.str && <ComponentX /> /* no error */}
{props.maybeString && <ComponentX /> /* error */}
{props.maybeObj && <ComponentX /> /* no error */}
{props.num && <ComponentX /> /* error */}
</div>
);
}
You'll first need to install ESLint and @typescript-eslint/parser
:
$ yarn add --dev eslint @typescript-eslint/parser
Next, install eslint-plugin-jsx-falsy
:
$ yarn add --dev eslint-plugin-jsx-falsy
Note: If you installed ESLint globally (using yarn global
or npm install -g
) then you must also install eslint-plugin-jsx-falsy
globally.
Add jsx-falsy
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix. Note that the rule won't work unless project
is specified in parserOptions
, since this rule uses type information (more details here).
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"]
},
"plugins": ["jsx-falsy"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"jsx-falsy/no-falsy-and": "error"
}
}
FAQs
Avoid accidentally rendering falsy values in your JSX.
The npm package eslint-plugin-jsx-falsy receives a total of 2,161 weekly downloads. As such, eslint-plugin-jsx-falsy popularity was classified as popular.
We found that eslint-plugin-jsx-falsy 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.
Product
Socket is introducing a new way to organize repositories and apply repository-specific security policies.
Research
Security News
Socket researchers uncovered malicious npm and PyPI packages that steal crypto wallet credentials using Google Analytics and Telegram for exfiltration.
Product
Socket now supports .NET, bringing supply chain security and SBOM accuracy to NuGet and MSBuild-powered C# projects.