-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] add no-render-return-undefined
: disallow components rendering undefined
#3750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
499e261
86d7ac8
61981c7
34b6a94
56e7436
9202a30
639f15f
8aec646
d1814c1
7905adf
b7ca1dd
f4983ae
0b56b09
94e9817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||
# Disallow returning undefined from react components (`react/no-render-return-undefined`) | ||||||
|
||||||
💼 This rule is enabled in the ☑️ `recommended` [config](https://github.com/jsx-eslint/eslint-plugin-react/#shareable-configs). | ||||||
|
||||||
<!-- end auto-generated rule header --> | ||||||
|
||||||
> In React 18, components may render undefined, and React will render nothing to the DOM instead of throwing an error. However, accidentally rendering nothing in a component could still cause surprises. This rule will warn if the `return` statement in a React Component returns undefined. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Issue: [#3020](https://github.com/jsx-eslint/eslint-plugin-react/issues/3020) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not sure linking to the issue is needed |
||||||
|
||||||
## Rule Details | ||||||
|
||||||
This rule will warn if the `return` statement in a React component returns undefined. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Examples of **incorrect** code for this rule: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add one that uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping |
||||||
|
||||||
```jsx | ||||||
function App() { | ||||||
return undefined; | ||||||
} | ||||||
|
||||||
// OR | ||||||
|
||||||
let ui; | ||||||
function App() { | ||||||
return ui; | ||||||
} | ||||||
|
||||||
// OR | ||||||
|
||||||
function getUI() { | ||||||
if (condition) return <h1>Hello</h1>; | ||||||
} | ||||||
function App() { | ||||||
return getUI(); | ||||||
} | ||||||
Comment on lines
+33
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this won't be possible across files, only if it's in the same file, so unless the function provides return type information (ie, TS) i don't think this is a check we can reliably do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to continue without supporting this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ljharb Can we do something here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I don't think we necessarily have to support this - I'm saying that the docs should be updated to reflect the caveats. |
||||||
``` | ||||||
|
||||||
Examples of **correct** code for this rule: | ||||||
|
||||||
```jsx | ||||||
function App() { | ||||||
return <div />; | ||||||
} | ||||||
|
||||||
// OR | ||||||
|
||||||
let ui = <div />; | ||||||
function App() { | ||||||
return ui; | ||||||
} | ||||||
|
||||||
// OR | ||||||
|
||||||
function getUI() { | ||||||
if (condition) return <h1>Hello</h1>; | ||||||
return null; | ||||||
} | ||||||
function App() { | ||||||
return getUI(); | ||||||
} | ||||||
``` |
Uh oh!
There was an error while loading. Please reload this page.