gpt4 book ai didi

javascript - React 中出现错误 "PropTypes is not defined"

转载 作者:行者123 更新时间:2023-12-01 02:11:11 26 4
gpt4 key购买 nike

好吧,这段代码在控制台中抛出一个错误 - “PropTypes 未定义”,因此应用程序不会呈现。您知道配置甚至代码有什么问题吗?顺便说一句,如果重要的话我不会使用 eslint。

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

但是这按预期工作(在组件之后):

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

package.json配置:

{
"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"
}
}

.babelrc配置:

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

webpack.config.js

const path = require('path');

module.exports = {
entry: ['babel-polyfill','./src/app.js'],
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/,
query: {
presets: ['react', 'env', 'stage-3', 'stage-2', 'stage-1', 'stage-0']
}
}, {
test: /\.s?css$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
}]
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true
}
};

最佳答案

您这里有一个拼写错误:-

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

应该是:-

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

关于javascript - React 中出现错误 "PropTypes is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49761473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com