javascript - PropTypes 在 React 中不起作用

标签 javascript reactjs react-proptypes

我正在运行 React 16.2.0,我正在使用 PropTypes 15.6.1。我正在使用 ES6 语法和 Webpack。

我试图让 PropTypes 在我传递无效 Prop 时发出警告,但它不起作用。这是代码:

SimpleMessage.js

import React from "react"
import PropTypes from "prop-types"

class SimpleMessage extends React.Component {
    render() {
        return(
            <p>{this.props.message}</p>
        )
    }
}

SimpleMessage.propTypes = {
    message: PropTypes.func
}

export default SimpleMessage

index.js

import React from "react"
import ReactDOM from "react-dom"

import SimpleMessage from "./components/SimpleMessage"

window.React = React

ReactDOM.render(
    <SimpleMessage message={"Hello World"} />,
    document.getElementById("react-container")
)

webpack.config.js

var webpack = require("webpack")
var path = require("path")

process.noDeprecation = true

module.exports = {
    entry: "./src/index.js",
    output: {
        path: path.join(__dirname, 'dist', 'assets'),
        filename: "bundle.js",
        sourceMapFilename: 'bundle.map'
    },
    devtool: '#source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    presets: ['env', 'stage-0', 'react']
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader','css-loader', {
                    loader: 'postcss-loader',
                    options: {
                      plugins: () => [require('autoprefixer')]
                    }}]
            },
            {
                test: /\.scss/,
                use: ['style-loader','css-loader', {
                    loader: 'postcss-loader',
                    options: {
                      plugins: () => [require('autoprefixer')]
                    }}, 'sass-loader']
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: JSON.stringify("production")
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            warnings: false,
            mangle: false
        })
    ]

}

正如您可能注意到的那样,我正在传递一个字符串(“Hello World”),但检查 proptypes 中的函数。我没有收到 proptypes 的任何错误或警告,代码运行正常。

最佳答案

按预期工作:

Warning: Failed prop type: Invalid prop message of type string supplied to SimpleMessage, expected function.

请务必检查您的浏览器控制台,这是显示错误的地方。

https://codepen.io/anon/pen/paGYjm?editors=1111


还有:

You shouldn’t apply UglifyJsPlugin or DefinePlugin with 'production' value in development because they will hide useful React warnings, and make the builds much slower.

source

关于javascript - PropTypes 在 React 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49050730/

相关文章:

javascript - jQuery:通过单击删除对象会导致意外的循环

Javascript - 多维关联数组

reactjs - TypeScript - React 上下文不适用于新的 PropTypes 包

javascript - IntelliSense 不适用于我自己的 React 组件库

javascript - 我如何在 NodeJS 中解决 "TypeError: Converting circular structure to JSON"

javascript - 无论类型是否正确,PropTypes 都会出错

javascript - JS 或 jQuery - 从数组中删除元素

javascript - 具有滑动功能的导航中的 anchor 链接跳得太远

javascript - 类型错误:更新后无法读取未定义的属性 'any'

javascript - ReactJS 应用程序正确处理输入值