|
| 1 | +# Prevent problematic leaked values from being rendered (react/jsx-no-leaked-render) |
| 2 | + |
| 3 | +Using the `&&` operator to render some element conditionally in JSX can cause unexpected values being rendered, or even crashing the rendering. |
| 4 | + |
| 5 | + |
| 6 | +## Rule Details |
| 7 | + |
| 8 | +This rule aims to prevent dangerous leaked values from being rendered since they can cause unexpected values reaching the final DOM or even crashing your render method. |
| 9 | + |
| 10 | +In React, you might end up rendering unexpected values like `0` or `NaN`. In React Native, your render method will crash if you render `0`, `''`, or `NaN`: |
| 11 | + |
| 12 | +```jsx |
| 13 | +const Example = () => { |
| 14 | + return ( |
| 15 | + <> |
| 16 | + {0 && <Something/>} |
| 17 | + {/* React: renders undesired 0 */} |
| 18 | + {/* React Native: crashes 💥 */} |
| 19 | + |
| 20 | + {'' && <Something/>} |
| 21 | + {/* React: renders nothing */} |
| 22 | + {/* React Native: crashes 💥 */} |
| 23 | + |
| 24 | + {NaN && <Something/>} |
| 25 | + {/* React: renders undesired NaN */} |
| 26 | + {/* React Native: crashes 💥 */} |
| 27 | + </> |
| 28 | + ) |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +This can be avoided by: |
| 33 | +- casting the condition to bool: `{!!someValue && <Something />}` |
| 34 | +- transforming the binary expression into a ternary expression which returns `null` for falsy values: `{someValue ? <Something /> : null}` |
| 35 | + |
| 36 | +This rule is autofixable, check the Options section to read more about the different strategies available. |
| 37 | + |
| 38 | +Examples of **incorrect** code for this rule: |
| 39 | + |
| 40 | +```jsx |
| 41 | +const Component = ({ count, title }) => { |
| 42 | + return <div>{count && title}</div> |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +```jsx |
| 47 | +const Component = ({ count }) => { |
| 48 | + return <div>{count && <span>There are {count} results</span>}</div> |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +```jsx |
| 53 | +const Component = ({ elements }) => { |
| 54 | + return <div>{elements.length && <List elements={elements}/>}</div> |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +```jsx |
| 59 | +const Component = ({ nestedCollection }) => { |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + {nestedCollection.elements.length && <List elements={nestedCollection.elements} />} |
| 63 | + </div> |
| 64 | + ) |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +```jsx |
| 69 | +const Component = ({ elements }) => { |
| 70 | + return <div>{elements[0] && <List elements={elements}/>}</div> |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +```jsx |
| 75 | +const Component = ({ numberA, numberB }) => { |
| 76 | + return <div>{(numberA || numberB) && <Results>{numberA+numberB}</Results>}</div> |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +```jsx |
| 81 | +// If the condition is a boolean value, this rule will report the logical expression |
| 82 | +// since it can't infer the type of the condition. |
| 83 | +const Component = ({ someBool }) => { |
| 84 | + return <div>{someBool && <Results>{numberA+numberB}</Results>}</div> |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Examples of **correct** code for this rule: |
| 89 | + |
| 90 | +```jsx |
| 91 | +const Component = ({ elements }) => { |
| 92 | + return <div>{elements}</div> |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```jsx |
| 97 | +// An OR condition it's considered valid since it's assumed as a way |
| 98 | +// to render some fallback if the first value is falsy, not to render something conditionally. |
| 99 | +const Component = ({ customTitle }) => { |
| 100 | + return <div>{customTitle || defaultTitle}</div> |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +```jsx |
| 105 | +const Component = ({ elements }) => { |
| 106 | + return <div>There are {elements.length} elements</div> |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +```jsx |
| 111 | +const Component = ({ elements, count }) => { |
| 112 | + return <div>{!count && 'No results found'}</div> |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +```jsx |
| 117 | +const Component = ({ elements }) => { |
| 118 | + return <div>{!!elements.length && <List elements={elements}/>}</div> |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +```jsx |
| 123 | +const Component = ({ elements }) => { |
| 124 | + return <div>{Boolean(elements.length) && <List elements={elements}/>}</div> |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +```jsx |
| 129 | +const Component = ({ elements }) => { |
| 130 | + return <div>{elements.length > 0 && <List elements={elements}/>}</div> |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +```jsx |
| 135 | +const Component = ({ elements }) => { |
| 136 | + return <div>{elements.length ? <List elements={elements}/> : null}</div> |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### Options |
| 141 | + |
| 142 | +The supported options are: |
| 143 | + |
| 144 | +### `validStrategies` |
| 145 | +An array containing `"cast"`, `"ternary"` or both (default: `["ternary", "cast"]`) - Decide which strategies are considered valid to prevent leaked renders (at least 1 is required). The "cast" option will cast to boolean the condition of the JSX expression. The "ternary" option transforms the binary expression into a ternary expression returning `null` for falsy values. The first option from the array will be used as autofix, so the order of the values matter. |
| 146 | + |
| 147 | +It can be set like: |
| 148 | +```json5 |
| 149 | +{ |
| 150 | + // ... |
| 151 | + "react/jsx-no-leaked-render": [<enabled>, { "validStrategies": ["ternary", "cast"] }] |
| 152 | + // ... |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +Assuming the following options: `{ "validStrategies": ["ternary"] }` |
| 157 | + |
| 158 | +Examples of **incorrect** code for this rule, with the above configuration: |
| 159 | +```jsx |
| 160 | +const Component = ({ count, title }) => { |
| 161 | + return <div>{count && title}</div> |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +```jsx |
| 166 | +const Component = ({ count, title }) => { |
| 167 | + return <div>{!!count && title}</div> |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +Examples of **correct** code for this rule, with the above configuration: |
| 172 | +```jsx |
| 173 | +const Component = ({ count, title }) => { |
| 174 | + return <div>{count ? title : null}</div> |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +Assuming the following options: `{ "validStrategies": ["cast"] }` |
| 179 | + |
| 180 | +Examples of **incorrect** code for this rule, with the above configuration: |
| 181 | +```jsx |
| 182 | +const Component = ({ count, title }) => { |
| 183 | + return <div>{count && title}</div> |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +```jsx |
| 188 | +const Component = ({ count, title }) => { |
| 189 | + return <div>{count ? title : null}</div> |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Examples of **correct** code for this rule, with the above configuration: |
| 194 | +```jsx |
| 195 | +const Component = ({ count, title }) => { |
| 196 | + return <div>{!!count && title}</div> |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +## When Not To Use It |
| 201 | + |
| 202 | +If you are working in a typed-codebase which encourages you to always use boolean conditions, this rule can be disabled. |
| 203 | + |
| 204 | +## Further Reading |
| 205 | + |
| 206 | +- [React docs: Inline If with Logical && Operator](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator) |
| 207 | +- [Good advice on JSX conditionals - Beware of zero](https://thoughtspile.github.io/2022/01/17/jsx-conditionals/) |
| 208 | +- [Twitter: rendering falsy values in React and React Native](https://twitter.com/kadikraman/status/1507654900376875011?s=21&t=elEXXbHhzWthrgKaPRMjNg) |
0 commit comments