_this2.setState is not a function on react asynchonous

Asked
Active3 hr before
Viewed126 times

8 Answers

functionreact
90%

this2.setState is not a function, Meta Stack Overflow , Stack Overflow Public questions & answers

The way you solved it works by simply avoiding the binding if this altogether, but your original problem was that the line

    this.getFormattedNameFromBackend.bind(this);
load more v
88%

Search Answer Titles

Pretag
 Pretag team - issue, fix, solve, resolve
72%

mean that you don't have the react class context in your function,for exemple on the onPress you can do like :,To bind your react class context into your function

_this2.setState is not a
function
load more v
65%

class App extends React.Component {
      state = {
            images: []
         }...async onSearchSubmit(term) {
               const response = await axios.get('https://api.unsplash.com/search/photos', {
                        params: {
                           query: term
                        },
                        headers: {
                           Authorization: 'Client-ID    }  })this.setState({ images: response.data.results });console.log('
                           onSearchSubmit this: ', this)}...
load more v
75%

March 14, 2020 - 7 min read,If you are a react developer and using a class component, you probably faced this error at least once:,TypeError: this.setState is not a function

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  countUp() {
    this.setState(currentState => {
      return { count: currentState.count + 1 };
    });
  }

  render() {
    const { count } = this.state;
    return (
      <div>
        <button onClick={this.countUp}>{count}</button>
      </div>
    );
  }
}
load more v
40%

_this2.setState is not a function错误解决办法 4292 ,一般的函式呼叫(Function Invocation Pattern), CLEARTEXT communication to XX not permitted by network security policy 解决方案 4711

import React,{Component} from 'react';
import {
    View,
    StyleSheet,
    Button,
    Text
} from 'react-native';

import * as StringUtilsService from '../Utils/StringUtilsService';

import * as InterfaceService from '../Utils/InterfaceService';

export default class StudyComponent extends Component{
    constructor(props){
        super(props);
        this.state = {
            jobName: null,
        };
    }

    render(){
        const { navigate } = this.props.navigation;
        return(
            <View style={styles.container}>
                <Button
                    title='招聘首页接口测试'
                    onPress={this.login}/>
                <Text>{this.state.jobName}</Text>
            </View>
        );
    }

    login(){
        var plain = new Object();
        plain['mobile'] = 'xxxxxxxxxxx';
        InterfaceService.recruitmentRecommendation(plain,(detail)=>{
            // alert(detail['jobList'][0]['jobName']);
            this.setState({
                jobName:detail['jobList'][0]['jobName'],
            });
        });
    }
}
load more v
22%

reactjs - React Uncaught TypeError: _this2.setStat... , java - JavaFx draw line on text area component - , xml - XSD complexContent restriction -

i've checked on stackoverflow error , results i've been getting functions needs binded this. have done in code , error still persists. cant figure out why happening. full code on codepen , shorted copy below

class maincomponent extends react.component{   constructor(props) {     super(props);     this.setoperand = this.setoperand.bind(this);     this.calculate = this.calculate.bind(this);     this.updatevalue = this.updatevalue.bind(this);     this.state = {       currentvalue: 0,       secondvalue: 0,       operand: null,       isnewnum: false     };   }    setoperand = (value) => {     //set data input   };    calculate = () => {     //math stuff     this.setstate = ({       currentvalue: result,       isnewnum: false     });   };    updatevalue = (value) => {     if(!this.state.isnewnum){ //first number       var newnum = "";       //set values       this.setstate({         currentvalue: newnum       });     } else {       var newnum2 = "";       //set values       this.setstate({         secondvalue: newnum2       });     }   };    render(){     return(       <div id="main">         <p>isnewnum = {`${this.state.isnewnum}`}</p>          {this.state.isnewnum ?  <display display ={this.state.secondvalue}/> : <display display ={this.state.currentvalue}/>}         <div>           <buttons value={1} onclick={this.updatevalue}/>           ...           <buttons value={'+'} onclick={this.setoperand}/>         </div>       </div>     )   }; }   reactdom.render(<maincomponent />, document.getelementbyid("root")); 
load more v
60%

Pretag
 Pretag team - issue, fix, solve, resolve

Other "function-react" queries related to "_this2.setState is not a function on react asynchonous"