question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Using boolean short circuit to conditionally render a component produces error when variable is undefined.

See original GitHub issue
export 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:closed
  • Created 3 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Eddie-CooRocommented, May 21, 2020

@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 the undefined as the return type of the components, especially in complicated conditional renders. But null is always intentional.

1reaction
gaearoncommented, Aug 20, 2020

I should note there is also another problem in your original example:

export default function App() {
  const item = undefined;
  const Item = () => item && <div>{item.name}</div>;

  return (
    <div>
      <Item />
    </div>
  );
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found