2017-04-09 62 views
13

我使用create-react-app開始React項目。 在最新React 15.5.3包,它出現以下警告:如何使用時修復陣營15.5.3 PropTypes過時警告創建反應的應用程序內

警告:訪問通過主PropTypes陣營包已被棄用。 改爲使用npm中的prop-types包。

我已經按照blog

npm install prop-typesimport PropTypes from 'prop-types';

,但它不工作。 我不使用任何PropTypesprops代碼:

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

class App extends Component { 
    constructor() { 
     super(); 
     this.state = { 
      videoVisible: true, 
     }; 
    } 

    ...... 
} 

如何解決呢?

謝謝。

回答

33

從反應博客拉 - NPM安裝道具類型,然後使用新的代碼。此外,它表示,如果嵌套組件不使用prop-types,但父組件是 - ,則可以獲取此錯誤消息,因此您需要檢查其他組件。

// Before (15.4 and below) 
import React from 'react'; 

class Component extends React.Component { 
    render() { 
    return <div>{this.props.text}</div>; 
    } 
} 

Component.propTypes = { 
    text: React.PropTypes.string.isRequired, 
} 

// After (15.5) 
import React from 'react'; 
import PropTypes from 'prop-types'; 

class Component extends React.Component { 
    render() { 
    return <div>{this.props.text}</div>; 
    } 
} 

Component.propTypes = { 
    text: PropTypes.string.isRequired, 
}; 
+1

謝謝。但是我已經關注這個博客,並且我沒有在代碼中使用任何道具,沒有運氣。 – Contra

+2

你可以把代碼放在github上嗎?也可以不使用道具,但其他庫仍然可以使用,例如Redux。 –

+2

哦,是的,正如你所說的,我使用aframe反應,它使用道具。我修正了這個問題。 謝謝! – Contra

相關問題