Skip to content

propType [name] is not required, but has no corresponding defaultProp declaration #1433

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

Closed
ghost opened this issue Sep 14, 2017 · 6 comments
Closed

Comments

@ghost
Copy link

ghost commented Sep 14, 2017

I am using Flow for type checking, and I have something like this:

type Props = {
  name?: ?string, // this means that "name" is NOT mandatory, and its value can be **null** or **undefined**
};

So, my question is, should I get this linting error? Is this the desired behavior?

@jseminck
Copy link
Contributor

I would say this is as expected. Since the prop is not required, you should specify a default value. You can decide to set it to null or undefined by default.

@ghost
Copy link
Author

ghost commented Sep 15, 2017

I see. Thank you

@ghost ghost closed this as completed Sep 15, 2017
@AlexanderNaiden
Copy link

AlexanderNaiden commented Dec 21, 2017

I didn't understand what should I do in my case (it's also shows eslint error: propType "isSmall" is not required, but has no corresponding defaultProp declaration. (react/require-default-props)):

type SwitcherProps = {
    isChecked: boolean,
    action: () => void,
    isSmall?: boolean
}

export const Switcher = (props: SwitcherProps) => {
    return (       
        <label className={"switcher ".concat(props.isSmall ? "small" : "")}>
            <input type="checkbox" checked={props.isChecked} className="input" />
            <span role="presentation" className="slider" onClick={props.action} />
        </label>
    );
};

@ljharb
Copy link
Member

ljharb commented Dec 21, 2017

@AlexanderNaiden add:

Switcher.defaultProps = {
  isSmall: false,
};

@arel
Copy link

arel commented Nov 30, 2018

The error message is confusing because it's singular instead of plural.

(and... #2063 was posted just 2 hours ago!)

@tanishbansal20
Copy link

tanishbansal20 commented Aug 20, 2019

I fixed this issue using the default prop

  static defaultProps = {
    actions: {
      function_name: () => null,
    },
  }

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants