获取错误" PropTypes未定义"在React

时间:2018-04-10 19:25:00

标签: javascript reactjs webpack babel react-proptypes

好吧,这段代码在控制台中引发错误 - " PropTypes未定义",因此应用程序无法呈现。任何想法在配置甚至代码中有什么问题?如果重要的话,我不会使用eslint。

<div>
  <% for(var i=0; i<data.length; i++) {%>
     <div class="tweet">
       <p class="tweetTime"><%= data[i].unix_timestamp%></p>
       <p class="tweetName"><%= data[i].screen_name%></p>
       <p class="tweetMessage"><%= data[i].cleaned_tweet%></p>
       <p class="tweetLocation"><%= data[i].user_location%></p>
     </div>
  <% } %>
</div>

然而,这可以按预期工作(在组件之后):

import React, { Component } from 'react';
import PropTypes from 'prop-types';

export default class Movie extends Component {
  static propTypes = {
    movie: Proptypes.shape({
      title: Proptypes.string.isRequired
    })
  };
  static defaultProps = {
    description: 'Description is not available'
  }
  render() {
    return (
      <div>
        <h3>{this.props.movie.title}</h3>
      </div>
    )
  }
}

package.json配置:

Movie.propTypes = {
  movie: PropTypes.shape({
    title: PropTypes.string.isRequired
  })
}

.babelrc配置:

{
  "dependencies": {
    "babel-polyfill": "^6.26.0",
    "live-server": "^1.2.0",
    "normalize.css": "^8.0.0",
    "prop-types": "^15.6.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^0.28.11",
    "node-sass": "^4.8.3",
    "sass-loader": "^6.0.7",
    "style-loader": "^0.20.3",
    "webpack": "^4.3.0",
    "webpack-cli": "^2.0.13"
  }
}

webpack.config.js

{
  "presets": [
    "env",
    "react",
    "stage-0"
  ],
  "plugins": [
    "transform-class-properties"
  ]
}

1 个答案:

答案 0 :(得分:1)

你这里有一个错字: -

static propTypes = {
    movie: Proptypes.shape({
      title: Proptypes.string.isRequired
    })
  };

应该是: -

static propTypes = {
    movie: PropTypes.shape({
      title: PropTypes.string.isRequired
    })
  };
相关问题