【问题标题】:React-Router npm package giving me warning in console? (PropTypes deprecated)React-Router npm 包在控制台中给我警告? (不推荐使用 PropTypes)
【发布时间】:2017-09-04 08:46:22
【问题描述】:

我目前正在使用 create-react-app 创建一个非常基本的 React 应用程序,代码如下:

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router';

import createBrowserHistory from 'history/createBrowserHistory';

import App from './App/AppComponent';

const browserHistory = createBrowserHistory();

ReactDOM.render(
    <Router path="/" history={browserHistory}>
        <div>
            <Route path="/" component={App} />
        </div>
    </Router>, document.getElementById('root')
);

AppComponent.js

import React, { Component } from 'react';

class App extends Component {
    render() {
        return (
            <div>This is the App component</div>
        );
    }
}

export default App;

当我运行此应用程序时,我在控制台中收到以下警告:

bundle.js:11888 Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.

当我删除 react-router 相关的东西时,我似乎没有收到控制台警告,所以我猜它来自那个包......但我之前在另一个小应用程序中使用过它并且没有有错误所以我有点困惑。

我在这里错过了什么?

【问题讨论】:

    标签: javascript reactjs react-router react-redux


    【解决方案1】:

    这是 React 在 React v15.5. 中所做的最近更改(2 天前),更改是 PropTypes 不再包含在 React 本身中,而是有自己的包 (prop-types)。这意味着任何使用React.PropTypes 的库(其中大多数)现在都显示此警告。已经向React Router 提交了一个问题,并且还提交了一些拉取请求。我想这将在接下来的 24-48 小时内修复。所以现在,忽略它。您的代码实际上没有任何问题。

    【讨论】:

    • 我想补充一下,它们不是投掷。这是一个警告,而不是错误。所以代码一直工作到 React 16。
    猜你喜欢
    • 2017-10-02
    • 2017-03-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多