React - uncaught TypeError: Cannot read property 'setState' of undefined

后端 未结 18 2379
清歌不尽
清歌不尽 2020-11-22 13:35

I am getting the following error

Uncaught TypeError: Cannot read property \'setState\' of undefined

even after binding delta in

相关标签:
18条回答
  • 2020-11-22 14:30

    You dont have to bind anything, Just use Arrow functions like this:

    class Counter extends React.Component {
        constructor(props) {
            super(props);
    
            this.state = {
                count: 1
            };
    
        }
        //ARROW FUNCTION
        delta = () => {
            this.setState({
                count: this.state.count++
            });
        }
    
        render() {
            return (
                <div>
                    <h1>{this.state.count}</h1>
                    <button onClick={this.delta}>+</button>
                </div>
            );
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:32

    if your are using ES5 syntax then you need to bind it properly

    this.delta = this.delta.bind(this)
    

    and if you are using ES6 and above you can use arrow function, then you don't need to use bind() it

    delta = () => {
        // do something
      }
    
    0 讨论(0)
  • 2020-11-22 14:34

    you have to bind new event with this keyword as i mention below...

    class Counter extends React.Component {
        constructor(props) {
            super(props);
    
            this.state = {
                count : 1
            };
    
            this.delta = this.delta.bind(this);
        }
    
        delta() {
            this.setState({
                count : this.state.count++
            });
        }
    
        render() {
            return (
                <div>
                    <h1>{this.state.count}</h1>
                    <button onClick={this.delta}>+</button>
                </div>
            );
          }
        }
    
    0 讨论(0)
  • 2020-11-22 14:37

    Adding

    onClick={this.delta.bind(this)}

    will solve the problem . this error comes when we try to call the function of ES6 class , So we need to bind the method.

    0 讨论(0)
  • 2020-11-22 14:38
    1. Check state check state whether you create particular property or not

    this.state = {
                name: "",
                email: ""
                }
                
               
                
    this.setState(() => ({ 
                 comments: comments          //comments not available in state
                 })) 

    2.Check the (this) if you doing setState inside any function (i.e handleChange) check whether the function bind to this or the function should be arrow function .

    ## 3 ways for binding this to the below function##

    //3 ways for binding this to the below function
    
    handleNameChange(e) {  
         this.setState(() => ({ name }))
        }
        
    // 1.Bind while callling function
          onChange={this.handleNameChange.bind(this)}
          
          
    //2.make it as arrow function
         handleNameChange((e)=> {  
         this.setState(() => ({ name }))
         })
        
    //3.Bind in constuctor 
    
    constructor(props) {
            super(props)
            this.state = {
                name: "",
                email: ""
            }
            this.handleNameChange = this.handleNameChange.bind(this)
            }

    0 讨论(0)
  • 2020-11-22 14:39
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <title>Hello World</title>
    
        <script src="https://unpkg.com/react@0.14.8/dist/react.min.js"></script>
        <script src="https://unpkg.com/react-dom@0.14.8/dist/react-dom.min.js"></script>
        <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
    
      </head>
      <body>
      <div id="root"></div>
        <script type="text/babel">
    
            class App extends React.Component{
    
                constructor(props){
                    super(props);
                    this.state = {
                        counter : 0,
                        isToggle: false
                    }
                this.onEventHandler = this.onEventHandler.bind(this);   
                }
    
                increment = ()=>{
                    this.setState({counter:this.state.counter + 1});
                }
    
                decrement= ()=>{
                    if(this.state.counter > 0 ){
                    this.setState({counter:this.state.counter - 1});    
                    }else{
                    this.setState({counter:0});             
                    }
                }
                // Either do it as onEventHandler = () => {} with binding with this  // object. 
                onEventHandler(){
                    this.setState({isToggle:!this.state.isToggle})
                    alert('Hello');
                }
    
    
                render(){
                    return(
                        <div>
                            <button onClick={this.increment}> Increment </button>
                            <button onClick={this.decrement}> Decrement </button>
                            {this.state.counter}
                            <button onClick={this.onEventHandler}> {this.state.isToggle ? 'Hi':'Ajay'} </button>
    
                        </div>
                        )
                }
            }
            ReactDOM.render(
            <App/>,
            document.getElementById('root'),
          );
        </script>
      </body>
      </html>
    
    0 讨论(0)
提交回复
热议问题