Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.JS Cannot read property 'props' of null Error

just started using react.js.

i keep having the same error. don't know why ?
the error is: Uncaught TypeError: Cannot read property 'props' of null
i know that the error happens when i click the button. specifically i think it's in the update() method.

JS Code:

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'))
like image 512
Ahmed Badawy Avatar asked Mar 04 '26 21:03

Ahmed Badawy


1 Answers

In this case you need pass to onClick do_update instead of update,

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

or change name from do_update to update

this.update = this.update.bind(this);
like image 143
Oleksandr T. Avatar answered Mar 07 '26 11:03

Oleksandr T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!