V
V
Vladimir Golub2019-10-16 13:10:04
JavaScript
Vladimir Golub, 2019-10-16 13:10:04

How to check data types of values ​​passed to a function (prop-types)?

How to check data types of values ​​passed to a function (prop-types) ?
There is a PropTypes.func type, but how do I check the types of the values ​​passed to the function?
Isn't there something like PropTypes.shape ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2018-07-26
@freeExec

Geocode the address.

A
Alexey, 2019-10-16
@RazerVG

There is no built-in validation of function parameters in prop types, but you can write a custom validator:

customProp: function(props, propName, componentName) {
    if (!/matchme/.test(props[propName])) {
      return new Error(
        'Проп `' + propName + '` компонента' +
        ' `' + componentName + '` имеет неправильное значение'
      );
    }
  }

Documentation here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question