继续得到这个错误,而试图写一个应用程序与第三方API工作

TypeError:这个。setState不是一个函数

当我试图处理API响应时。我怀疑这个装订有问题,但我不知道怎么修。这是我的组件的代码:

var AppMain = React.createClass({
    getInitialState: function() {
        return{
            FirstName: " "
        };
    },
    componentDidMount:function(){
        VK.init(function(){
            console.info("API initialisation successful");
            VK.api('users.get',{fields: 'photo_50'},function(data){
                if(data.response){
                    this.setState({ //the error happens here
                        FirstName: data.response[0].first_name
                    });
                    console.info(this.state.FirstName);
                }

            });
        }, function(){
        console.info("API initialisation failed");

        }, '5.34');
    },
    render:function(){
        return (
            <div className="appMain">
            <Header  />
            </div>
        );
    }
});

当前回答

您只需要绑定您的事件

ex -

// place this code to your constructor

this._handleDelete = this._handleDelete.bind(this);

// and your setState function will work perfectly

_handleDelete(id){

    this.state.list.splice(id, 1);

    this.setState({ list: this.state.list });

    // this.setState({list: list});

}

其他回答

React建议在所有需要使用this类的方法中绑定this,而不是self函数的this。

constructor(props) {
    super(props)
    this.onClick = this.onClick.bind(this)
}

 onClick () {
     this.setState({...})
 }

或者你可以用箭头函数代替。

您只需要绑定您的事件

ex -

// place this code to your constructor

this._handleDelete = this._handleDelete.bind(this);

// and your setState function will work perfectly

_handleDelete(id){

    this.state.list.splice(id, 1);

    this.setState({ list: this.state.list });

    // this.setState({list: list});

}

使用from arrow函数来处理动作。

I Just Pass Props from child component to the parent component in react native from the child component is pass this.
 const validate = (text) => {
        console.log(text);
        let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;
        if (reg.test(text) === false) {
          console.log("Email is Not Correct");
        
          ({ onChangeText: text })
          return false;
        }
        else {
          // console.log("Email Correct");
          
          props.onchangex(text)<<----------------------- here pass the value to the parent component
          console.log(props.onchangex(text));
          ({ onChangeText: text })
       

        }

 and in parent component i set this value to the state like this
   <InpE
      onchangex={this.propx}<---------- pass the parent function to the child component
      placeholder="Please Enter Email"
          />

propx=(a)=> {
 this.setState({email:a}) <-------- this gives me error TypeError: this.setState is not a function


       {this.state.email=a} <---------------- this line is setting up the state email variable
        
    }

你可以使用ES6的箭头函数避免使用.bind(this)。

VK.api('users.get',{fields: 'photo_50'},(data) => {
        if(data.response){
            this.setState({ //the error happens here
                FirstName: data.response[0].first_name
            });
            console.info(this.state.FirstName);
        }

    });