Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

reactjs - 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

question from:https://stackoverflow.com/questions/66066119/react-proptypes-file-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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) 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...