React propType cannot read undefined property

Using propTypes to verify props gives the following error:

TypeError: Cannot read the 'string' property from undefined.

TypeError: Unable to read the func property from undefined.

This code is at the bottom of the snippet:

import React from 'react'; import ProjectItem from './ProjectItem'; class Projects extends React.Component { deleteProject(title) { this.props.onDelete(title); } render() { let projectItems; if (this.props.project) { projectItems = this.props.project.map(project => { return ( <ProjectItem key={project.title} project={project} onDelete={this.deleteProject.bind(this)} /> ) }); } return ( <div className="Projects"> {projectItems} </div> ); } } Projects.propTypes = { projects: React.PropTypes.string, onDelete: React.PropTypes.func } 
+5
source share
1 answer

You need to install prop-types package and then add import statement

import PropTypes from prop-types;

at the top of your class.

PropTypes have been ported from React to prop-types proprietary package.

EDIT: as stated in the comments, this only applies to React version 15.5 and higher.

+7
source

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


All Articles