我创建了一个组件ChannelSection.jsx,它不是最外面的组件,App.jsx将是最外面的组件。我的ChannelSection需要从其父组件接收道具。所以添加了下面的道具类型:
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 :(得分:2)
确保在所有情况下都使用React.PropTypes
。它是复数和案例都很重要。
<强>更新强>
React.PropTypes
现已弃用。请使用具有默认导出prop-types
的{{1}} npm
包。
那些使用PropTypes
包中的PropTypes
的人,您可以使用react
来更新您的项目。