eslint-plugin-react: [BUG] react/prop-types - false positive when returning null

I’m trying to update 7.20.0 to 7.20.3, but I have new false positive errors for components that can return null.

This is telling me 'number' is missing in props validationeslintreact/prop-types:

import React from 'react';

type Props = {
  number: number;
};

export default (props: Props) => {
  const { number } = props;

  if (number === 0) {
    return <p>Number is 0</p>;
  }

  return null;
};

But this is fine:

import React from 'react';

type Props = {
  number: number;
};

export default (props: Props) => {
  const { number } = props;

  if (number === 0) {
    return <p>Number is 0</p>;
  }

  return <p>Number is not 0</p>;
};

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Anytime šŸ˜„ . I will make a pr after my latest pr is merged

Forget to tell I commit to my own fork. Here:https://github.com/hank121314/eslint-plugin-react/tree/develop

Specifically, component detection should be looking at every return value, and if any of them are jsx, and if all of them are possibly valid component return values (jsx, null, or a string/number/array of those things), then it should be detected as a component. (cc @alexzherdev)

@ljharb exactly, no issue if the null is returned in the condition