javascript - 未捕获的类型错误 : Cannot read property 'prop

  • 我有一个 react 代码
  • 此代码在 UI 中呈现各种面板。
  • 当我点击一个标签时,这个函数被称为sportsCornerPanel()
  • 但我得到了 Uncaught TypeError 如何解决它
  • 在下面提供代码段。
  • 您可以在 fiddle 中看到完整的代码

代码片段

    sportsCornerPanel() {
        debugger;

        console.log("sportsCornerPanel"
        console.log("this.props.sportsPanelState.size-->" + this.props);

        if (this.props.sportsPanelState.size === 'hidden') {

            if (!this.props.sportsPanelState.visible) {
                this.props.dispatch(sportsOpenPanel());
            } else {
                this.props.dispatch(sportsClosePanel());
            }
        }
    }


    render() {


        let sportsContent, leftNavLink;

        if (this.props.sports-layout !== 'small') {
            console.log("SportsBox---page loads at bigger size");
            console.log("SportsBox---page loads at ipad size");
            sportsContent = <SportsBox className="sports-header"/>;
        } else {
            if (this.props.sportsPanelState.visible) {
                console.log("sportsPanelState--when it becomes small--around ipad width");

                sportsContent = <SportsBox className="sports-nav"/>;
                leftNavLink = <a onClick={this.sportsCornerPanel} href="javascript:;" className="header-navLink active"></a>;
            } else {
                if (this.props.sports.active) {

                    console.log("SportsBox");

                    sportsContent = <SportsBox className="sports-nav"/>;
                } else {

                    console.log("leftNavLink--when it becomes small--around ipad width");

                    leftNavLink = <a onClick={this.sportsCornerPanel} href="javascript:;" className="header-navLink"></a>;
                }
            }
        }


output

Uncaught TypeError: Cannot read property 'props' of null

最佳答案

由于你没有在类方法中使用 React.createClass this 不引用组件实例,所以你应该手动绑定(bind)它。有几种方法:

1.在类构造函数中手动绑定(bind)this

constructor(props) {
    super(props);
    this.sportsCornerPanel= this.sportsCornerPanel.bind(this);
}

2。使用带有箭头函数的 ES7 属性初始化器

sportsCornerPanel = () => {
    debugger;

    console.log("sportsCornerPanel"
    console.log("this.props.sportsPanelState.size-->" + this.props);

    if (this.props.sportsPanelState.size === 'hidden') {

        if (!this.props.sportsPanelState.visible) {
            this.props.dispatch(sportsOpenPanel());
        } else {
            this.props.dispatch(sportsClosePanel());
        }
    }
}

3.在调用点绑定(bind) this

render()方法中:

    let sportsContent, leftNavLink;

    if (this.props.sports-layout !== 'small') {
        console.log("SportsBox---page loads at bigger size");
        console.log("SportsBox---page loads at ipad size");
        sportsContent = <SportsBox className="sports-header"/>;
    } else {
        if (this.props.sportsPanelState.visible) {
            console.log("sportsPanelState--when it becomes small--around ipad width");

            sportsContent = <SportsBox className="sports-nav"/>;
            leftNavLink = <a onClick={this.sportsCornerPanel.bind(this)} href="javascript:;" className="header-navLink active"></a>;
        } else {
            if (this.props.sports.active) {

                console.log("SportsBox");

                sportsContent = <SportsBox className="sports-nav"/>;
            } else {

                console.log("leftNavLink--when it becomes small--around ipad width");

                leftNavLink = <a onClick={this.sportsCornerPanel.bind(this)} href="javascript:;" className="header-navLink"></a>;
            }
        }
    }

关于javascript - 未捕获的类型错误 : Cannot read property 'props' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303490/

相关文章:

json - 浏览器 JSON 插件

java - 在spring-mvc中将json解析为java对象

json - Jackson:有没有办法将 POJO 直接序列化为 treemodel?

javascript - 如何在不解析的情况下在javascript中同步包含JSON数据?

c# - 使 ASP.NET WCF 将字典转换为 JSON,省略 "Key"和 "Value"标记

json - 如何解析 JSON 文件?

python - 无法解析 JSON 文件中的 TAB

json - 将 JSON 与 LogStash 一起使用

c# - 反序列化期间的 JSON.Net Ignore Property

php - 无法安装 Composer - 缺少 json 扩展名