Checking the PropTypes component is equivalent in Flow

Given the following code using PropTypes (see this question )

const SomeComponent = ({comp}) => {...}

SomeComponent.propTypes = { 
  comp: PropTypes.shape({
    type: PropTypes.oneOf([SomeOtherComponent])
  })
}

what is equivalent using stream types?

I just got to:

const SomeComponent = ({comp}: {comp: React$Element<any>}) => {...}

using this for reference, but it will allow to compbe any component of React.

How can I check the prop type to make sure it is an instance of a specific React component using Flow?

+4
source share
2 answers

Like stream 0.53, this can be done using React.Element<typeof Component>.

0
source

: . , Flow , , , React . , , , . Flow , , , Flow .

, , . , , " ". :

 function elementIsOfType(element, typeName) {
   if (!__DEV__) {
     return true;
   }
   return element.type.displayName === typeName;
 }

__DEV__ false, .

+2

Source: https://habr.com/ru/post/1676092/


All Articles