未捕获的TypeError:无法读取null的属性'props'

30 javascript json reactjs

  • 我有一个反应代码
  • 此代码在UI中呈现各种面板.
  • 当我点击一个标签时,这个函数叫做sportsCornerPanel()
  • 但是我得到了Uncaught TypeError如何修复它
  • 提供下面的代码段.
  • 整个代码你可以在小提琴中看到它

代码段

    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
Run Code Online (Sandbox Code Playgroud)

xCr*_*rZx 52

由于您没有React.createClass在类中使用方法this,因此不会引用组件实例,因此您应该手动绑定它.有几种方法:

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

constructor(props) {
    super(props);
    this.sportsCornerPanel= this.sportsCornerPanel.bind(this);
}
Run Code Online (Sandbox Code Playgroud)

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

3. 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>;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 要调试,只需在类方法的开头``console.log(this)`.如果它输出`null`或`undefined`,那么这就是问题所在. (4认同)
  • 当使用`extends React.Component`而不是`React.createClass()`时,这是常见的问题. (3认同)
  • 箭头函数与围绕它的代码具有相同的词汇"this",因此我们得到了期望的结果,因为属性初始化器范围. (2认同)