Search code examples
javascriptreactjsreact-proptypes

How to show warning if unknown prop is passed in react component?


Suppose my react component should be called like this

<comp propA='a' />

and i want to check using proptypes if someone says

<comp propA='a' propB='a' />

i should get a warning saying that propB can not be accepted

I DONT WANT TO USE A CUSTOM PROP TYPE IMPLEMENTATION just want to know if this propblem is already tackled?


Solution

  • The prop-types package does not currently support this. Here is an open issue requesting it: https://github.com/facebook/prop-types/issues/11.

    It looks like https://github.com/airbnb/prop-types does support this (I know you said you don't want a custom prop type implementation) with forbidExtraProps.

    Another option is using Typescript, which would enforce this at compile time rather than runtime.