javascript - 为什么无法读取PropType.func?

标签 javascript reactjs

我创建了一个组件ChannelSection.jsx,它不是最外面的组件,App.jsx将是最外面的组件。我的 ChannelSection 将需要从其父组件接收 props。因此添加了以下 Prop 类型:

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'));

最佳答案

确保在所有情况下都使用 React.PropTypes。复数和大小写都很重要。

更新

React.PropTypes 现已弃用。请使用 prop-types npm 包,它具有默认导出 PropTypes

如果使用 react 包中的 PropTypes,您可以使用 react-codemod 来更新您的项目。

关于javascript - 为什么无法读取PropType.func?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778161/

相关文章:

javascript - 粘性菜单在 iOS Safari 和 Chrome 上过早跳转到固定位置

Javascript 和 jQuery 在同一个 HTML 文件中

javascript - 使用 fetch POST 方法将文件发送到服务器 API

javascript - react-apollo gql, TypeError : Object(. ..) 不是函数

javascript - sveltejs - 组件的静态属性

javascript - Jquery, 'invalid or unexpected token'。不是字符串问题

javascript - 将 javascript 变量发送到框架中

reactjs - .tsx webpack 编译失败 : Unexpected token <

javascript - 浏览器选项卡处于非事件状态时如何在 REACT 应用程序中播放音频?

javascript - 沿 d3 中的路径拖动