2017-04-08 50 views
30

在最新陣營15.5.1包,如果使用巴貝爾反應用來解決JSX文件,將出現以下警告:通天禁用React.createClass和PropTypes過時的警告作出反應目前

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

warning.js:36 Warning: A Component: React.createClass is deprecated and will be removed in version 16. Use plain JavaScript classes instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement. 

我的代碼如下:

import React from 'react' 
import ReactDOM from 'react-dom'; 

class Counter extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     count: 1 
    } 
    } 
    componentDidMount() { 
    setInterval(()=> { 
     this.setState((prevState, props) => ({ 
     count: prevState.count + 1 
     })) 
    }, 1000) 
    } 
    render(){ 
    return (
     <h1>{this.state.count}</h1> 
    ) 
    } 
} 

const root = document.createElement('div'); 
root.id = 'app'; 
document.body.appendChild(root); 

ReactDOM.render(
    <Counter />, 
    document.querySelector('#app') 
); 

我不使用React.createClass和PropTypes在我的代碼,這似乎巴貝爾變換我的代碼React.createClassPropTypes,如何解決呢?

+0

您確定它來自此代碼嗎? babeljs.io似乎並不這麼認爲; [這個要點顯示你的來源和babeljs輸出](https://gist.github.com/davelnewton/495104605f24c588624f5b169075c091)。在https://babeljs.io/repl查看。你使用的是什麼版本的Babel? –

+1

我目前遇到與新安裝的npm軟件包相同的問題。我想這可能是一些突破性變化的結果?如果在開發控制檯中,棄用警告是黃色而不是紅色,那麼 –

+0

會很好。 –

回答

-3

你不應該延長react.Component。您需要導入組件從反應並擴展它在這裏顯示:

import React, {Component} from 'react'; 

    class Counter extends Component{ 
    //your code here 
    render(){ 

    return (
     <h1>{this.state.count}</h1> 
    ); 
    } 
} 

你可以閱讀有關的差異和ES6類here

+2

你可以解釋一下'class MyComponent extends React.Component'和'class MyComponent extends Component'嗎? @N。 Solomon –

+0

@SiddharthTrivedi - 如果您使用反應導入它,則可以使用Component。例如:從'react'導入React,{Component};' – Steph

+2

你基本上只是從反應中提取Component,因爲React.Component也是正確的。 –

0

這不是一個答案,只是一個增加更多的東西,因爲我還不能發表評論:-(也許別人有react-同樣的問題不受控制(下拉):React.createClass已被棄用,將在版本16被移除自舉

我在使用反應的自舉0.30.8連同反應15.5.3

警告得到了相同的。改用普通的JavaScript類。如果您尚未準備好遷移,則可以在npm上使用create-react-class作爲插入式替換。

i的獲得: 不可控/ createUncontrollable.js --->

var component = _react2.default.createClass(_extends({ 

[email protected]是的DEP反應的自舉0.30.8

警告:不推薦使用通過主React包訪​​問PropType。改爲使用npm中的prop-types包。

反應的自舉/ ES/PanelGroup.js --->

accordion: React.PropTypes.bool 

我想降級的反應是在這裏解決辦法,因爲反應的自舉尚未遠遠不夠的唯一途徑。

4

陣營v15.5.0實現new warnings

降級反應15.4。X作品對我來說

npm install --save [email protected] [email protected] 
+0

不可思議,通過更新到新版本5錯誤警告已經消失.. –

+0

@eugen_sunic - 版本5的是什麼?你更新了什麼? – Giladd

+0

反應dom包..它不是版本5,我說它刪除了5個錯誤 –

11

陣營15.5.0包含新的警告屬於那些上反應16未來的變化,你得到的警告,其實是告訴你,你必須實現新的方式(因爲您正在使用的當前方式將在16日棄用)。

React.createClass,你有兩個選擇,第一種是隻使用純JS語法:

function Welcome(props) { 
    return <h1>Hello, {props.name}</h1>; 
} 

或使用create-react-class模塊(在NPM availiable):

// Before (15.4 and below) 
var React = require('react'); 

var Component = React.createClass({ 
    mixins: [MixinA], 
    render() { 
    return <Child />; 
    } 
}); 

// After (15.5) 
var React = require('react'); 
var createReactClass = require('create-react-class'); 

var Component = createReactClass({ 
    mixins: [MixinA], 
    render() { 
    return <Child />; 
    } 
}); 

關於PropTypes警告,檢查你是否使用使用PropTypes的模塊,有時它來自外部。

無論如何,閱讀更多關於它,歡迎您獲得Facebook blog post about those changes

+0

您也可以使用es6類來定義組件 –

+0

最好的答案,因爲這是與開發工具控制檯中的建議點。通過主React軟件包訪問createClass已棄用,並將在React v16.0中刪除。改用普通的JavaScript類。如果您尚未準備好遷移,則可以在npm上使用create-react-class v15。*作爲臨時插入替換。 – Chad

3

我發現警告存在,因爲我是進口與

import * as React from 'react' 

改變這

import React from 'react' 
陣營

使警告消失

+0

使用Flow時需要其他進口。這是Jest的另一種解決方法:https://gist.github.com/turadg/9bcf08a7279e82a030a645250639fe6e – Turadg