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'));
标题说PropType是否确保在应用程序中无处使用PropType?您发布的代码片段看起来正确并正确使用PropTypes,但是您可能在其他地方使用PropType。 – kwelch
@kwelch,良好的调用,在ChannelForm.jsx我有一些小写的PropTypes。继续并发布您的答案作为答案,以便我可以检查它并感谢您的帮助。 – Daniel
很高兴帮助。 – kwelch