Search code examples
reactjsreact-proptypes

React PropTypes File type


How do I check if passed prop is of type File using Proptypes?

using PropTypes.instanceOf(File) fails with ReferenceError: File is not defined

Note: new File() constructor and typeOf someVar === File do not throw the mentioned reference error


Solution

  • Stemming from this: https://www.w3.org/TR/FileAPI/#file-section So what you could do is checking if the object has a property that is name and that is a string: something like this:

    const isFile = function(props, propName, componentName) {
      const retVal = typeof props[propName].name == 'string';
      return new Error(
            `Invalid prop ${propFullName} supplied to ${componentName}. It is not a File.`
          );
    }
    Component.propTypes = {
       onClick: isFile(1) 
    }