Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

null props considered differently in getDefaultProps vs. isRequired #110

Open
aweary opened this issue Sep 19, 2017 · 5 comments
Open

null props considered differently in getDefaultProps vs. isRequired #110

aweary opened this issue Sep 19, 2017 · 5 comments

Comments

@aweary
Copy link
Contributor

aweary commented Sep 19, 2017

Moving from the React repo, original issue: facebook/react#2166

@jharris4
Copy link

jharris4 commented Sep 20, 2017

Thanks for linking this issue! Interesting that 39 people reacted with a thumbs up to +1 for isDefined / isRequired

There's a lengthy discussion about the topic of null props going on in the PR: #90

@martpie
Copy link

martpie commented Apr 30, 2018

For those interested in a workaround, you can always define your own propTypes:

// Number or null
foo: function(props, propName, componentName) {
  if (props[propName] !== null || typeof props[propName] !== 'number')
    return new Error(
      'Invalid prop `' + propName + '` supplied to' +
      ' `' + componentName + '`. Validation failed.'
    );
}

And it is perfectly possible to make it re-usable as it is a function.

@virgofx
Copy link
Contributor

virgofx commented Apr 30, 2018

@KeitIG I think the issue is that people prefer an out of box solution to handle such a common use-case.

@martpie
Copy link

martpie commented Apr 30, 2018

I totally agree, I am just leaving this solution in case of someone not knowing how to tackle this in the meantime. (like me, this morning).

@ghost
Copy link

ghost commented Aug 26, 2021

The check should be

foo: function(props, propName, componentName) {
        if (props[propName] !== null && typeof props[propName] !== 'number')
            return new Error(
                'Invalid prop `' +
                    propName +
                    '` supplied to' +
                    ' `' +
                    componentName +
                    '`. Validation failed.'
            );
    },

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants