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.

PropType for function argument type checking.

See original GitHub issue

I’d really like to be able to define the type of arguments a prop function should be called with.

class Test extends Component {
  static propTypes = {
    foo: PropTypes.func(PropTypes.number).isRequired,
  }
  ...
}

I think that this is the last missing piece we need to really define a React component interface. I might not be the only one to feel the need to comment those signatures. But having a way to do it programmatically would be much better.

class Test extends Component {
  static propTypes = {
    foo: PropTypes.func.isRequired, //signature: (id: Number)
  }
  ...
}

First attempt

I tried to create a new PropTypes able to do that by wrapping the function given as prop:

function createFuncTypeChecker(...argumentsTypeCheckers) {
  function validate(props, propName, descriptiveName, location) {
    const propValue = props[propName];
    const propType = typeof propValue;
    if (propType !== 'function') {
      const locationName = location;
      return new Error(
        `Invalid ${locationName} \`${propName}\` of type \`${propType}\`
         supplied to \`${descriptiveName}\`, expected a function.`
      );
    }
    props[propName] = (...args) => {
      argumentsTypeCheckers.forEach((typeChecker, i) => {
        const error = typeChecker(args, i, `${descriptiveName} arguments`, location);
        if (error instanceof Error) {
          handleArgumentsTypeError(error);
        }
      });
      propValue.apply(this, args);
    };
    return null;
  }
  return createChainableTypeChecker(validate);
}

It’s working but I’m receiving the pretty straightforward warning message: Warning: Don't set .props.foo of the React component. Instead, specify the correct value when initially creating the element or use React.cloneElement to make a new element with updated props.. Yeh, mutating the prop is not a nice way but that’s the only way I found without modifying React core.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:10
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

41reactions
jitcodercommented, Mar 15, 2016

+1

17reactions
albclcommented, Mar 11, 2021

I think this is an excellent idea when PropTypes are used for type checking. Clearly when using TypeScript or Flow this won’t be a problem, but in that case there’s no need for PropTypes

I don’t see why you wouldn’t have PropTypes when also using TypeScript. Those PropTypes will validate at runtime while TypeScript will just do it when compiling. For me, they seem to do very different things.

I know this was closed years ago, but I still find this a really useful request. I think I’d be useful to give it a second thought at this whole idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >
ReactJS component PropTypes - specify a function type with a ...
As you can see my onClick prop not only needs to be a function but needs to accept 2 arguments. is there any...
Read more >
Type-Checking in React: PropTypes | by Chidume Nnamdi
React provides a way to warn us if a parent component passes in a parameter that is a different type of the expected...
Read more >
ReactJS Typechecking With PropTypes - Set 1 - GeeksforGeeks
React allows us to pass arguments to a Component using something called props (stands for properties) which are passed to components via ...
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