2016-11-24 33 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来更新您的项目。

相关问题