Bug: Using boolean short circuit to conditionally render a component produces error when variable is undefined.
See original GitHub issueexport default function App() {
const item = undefined;
const Item = () => item && <div>{item.name}</div>;
return (
<div>
<Item />
</div>
);
}
In our case, item is initialized from array.find. It is a react-bootstrap NavDropdown.Item with others in a NavDropdown. It produces a white screen in our app rather than the on-screen errors seen in the example, but the javascript console shows the same errors. Changing to ternary item ? <div>{item.name}</div> : null
works as a workaround.
React version: ^16.12.0
Link to code example: https://codesandbox.io/s/kind-voice-t2mdo
The current behavior
Error Item(…): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
The expected behavior
skip rendering, no errors, as it does if item is null.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
reactjs - React showing 0 instead of nothing with short-circuit ...
If the statement is false, it renders a 0 instead of nothing. I have done a console.log(profileTypesLoading) just to see quickly what the...
Read more >Conditional rendering in React Native may crash your app
Returning '' directly into ReactNative JSX will produce error Text strings must be rendered within a <Text> component and cause crash.
Read more >Stop Using “&&” for Conditional Rendering in React Without ...
To prevent avoidable UI bugs, use the JavaScript ternary operator for conditional rendering of React components instead of the logical AND operator.
Read more >Solved: React Conditional Rendering Renders '0' - Designcise
When the left-side of the logical expression is false , it short-circuit evaluates to the falsy expression, and the falsy expression is rendered...
Read more >React Clean Code - Simple ways to write better and cleaner ...
1. Conditional rendering only for one condition. If you need to conditionally render something when a condition is true and not render anything ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@illuminist I think it’s because forgetting to return something from a function in javascript, returns
undefined
by default. So React catches a lot of bugs early by not letting theundefined
as the return type of the components, especially in complicated conditional renders. Butnull
is always intentional.I should note there is also another problem in your original example:
Here you are generating a different
Item
component on every render which causes it to lose any state in the subtree. Don’t do that.