刚开始使用react.js。

我一直有同样的错误。不知道为什么吗?
错误是:Uncaught TypeError: Cannot read property 'props' of null我知道单击按钮时会发生错误。具体来说,我认为这是在update()方法中。

JS代码:

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component{
    constructor(){
        super();
        this.state = {increasing:false}
        this.do_update = this.update.bind(this);
    }
    update(){
        ReactDOM.render(
            <App val={this.props.val+1} />,
            document.getElementById('app')
        );
    }
    componentWillReceiveProps(nextProps){
        this.setState({increasing: (nextProps.val > this.props.val) })
    }
    render(){
        return <button onClick={this.update}>{this.props.val}</button>
    }
}

App.defaultProps = {val:0}
ReactDOM.render(<App />,document.getElementById('app'))

最佳答案

在这种情况下,您需要传递给onClick do_update而不是update

return <button onClick={ this.do_update }>{ this.props.val }</button>

或将名称从do_update更改为update
this.update = this.update.bind(this);

关于javascript - React.JS无法读取null的属性 'props'错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36406472/

10-12 00:15
查看更多