2016-11-24 76 views
0

我已經創建了一個不是最外面組件的ChannelSection.jsx組件,App.jsx將是最外面的組件。我的ChannelSection需要從其父組件接收道具。所以添加的道具類型吼叫:爲什麼無法讀取PropType.func?

ChannelSection.jsx:

import React, {Component} from 'react'; 
import ChannelForm from './ChannelForm.jsx'; 
import ChannelList from './ChannelList.jsx'; 

class ChannelSection extends Component { 
    render(){ 
     return (
      <div> 
       <ChannelList {...this.props} /> 
       <ChannelForm {...this.props} /> 
      </div> 
     ) 
    } 
} 

ChannelSection.propTypes = { 
    channels: React.PropTypes.array.isRequired, 
    setChannel: React.PropTypes.func.isRequired, 
    addChannel: React.PropTypes.func.isRequired 
} 

export default ChannelSection 

,我在控制檯收到此錯誤,我不知道爲什麼,我需要在此進行故障排除一些幫助:

Uncaught TypeError: Cannot read property 'func' of undefined 

我App.jsx文件:

import React, {Component} from 'react'; 
import ChannelSection from './channels/ChannelSection.jsx'; 

class App extends Component { 
    constructor(props){ 
     super(props); 
     this.state = { 
      channels: [] 
     }; 
    } 
    addChannel(name){ 
     let {channels} = this.state; 
     channels.push({id: channels.length, name}); 
     this.setState({channels}); 
     // TODO: Send to Server 
    } 
    setChannel(activeChannel){ 
     this.setState({activeChannel}); 
     // TODO: Get Channel Messages 
    } 
    render(){ 
     return (
      <ChannelSection 
       channels={this.state.channels} 
       addChannel={this.addChannel.bind(this)} 
       setChannel={this.setChannel.bind(this)} /> 
     ) 
    } 
} 

export default App 

我index.js文件:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './components/App.jsx'; 

ReactDOM.render(App, document.getElementById('root')); 
+0

標題說PropType是否確保在應用程序中無處使用PropType?您發佈的代碼片段看起來正確並正確使用PropTypes,但是您可能在其他地方使用PropType。 – kwelch

+0

@kwelch,良好的調用,在ChannelForm.jsx我有一些小寫的PropTypes。繼續併發布您的答案作爲答案,以便我可以檢查它並感謝您的幫助。 – Daniel

+0

很高興幫助。 – kwelch

回答

2

確保您在所有情況下都使用React.PropTypes。它是複數和案件都很重要。

更新

React.PropTypes現在已經過時。請使用prop-typesnpm包,該包有默認導出PropTypes

那些使用react包中的PropTypes,可以使用react-codemod來更新您的項目。

相關問題