Undefined is not an object evaluating this.state/this.props

前端 未结 4 920
心在旅途
心在旅途 2021-01-28 16:20

How do I bind a function outside of scope in React Native? I\'m getting the errors:

undefined is not an object evaluating this.state   

&

4条回答
  •  情深已故
    2021-01-28 16:26

    You are going in the right direction, but there is still a minor issue. You are passing a function to your map callback that has a different scope (this) than your component (because it is not an arrow function), so when you do bind(this), you are rebinding your callback to use the scope from map. I think this should work, it basically turns the callback that you pass to map into an arrow function. Also, since you bind your function in the constructor, you do not need to do it again:

      // The constructor logic remains the same
      // ....
      renderLoadingView() {
          return (
          
            
              Loading ...
            
          
          )
        }
    
      _buttonPress = () =>  {
          this.props.navigator.push({
            id: 'Main'
          })
      }
    
      renderGPSDataFromServer =() => {
    
        const {loaded} = this.state;
        const {state} = this.state;
    
        return this.state.dataArr.map((data, i) => {
          return(
            
    
              
                
                View Video
                
              
    
              
                {i}
                
                  
                  {Number(data.lat).toFixed(2)}
                  {Number(data.long).toFixed(2)}
                
                
                {this.calcRow(55,55).bind(this)}
                
              
    
            
          );
        });
      }
    
      render = ()=> {
        if (!this.state.loaded) {
          return this.renderLoadingView();
        }
        return(
          
            {this.renderGPSDataFromServer()}
          
        )
      }};
    

提交回复
热议问题