Change state through callback in RN

class testNetworkModel {
    constructor(callback) {
        this.callback = callback;
    }
    callback:Function = null;
getCheckCode = () => {
    
        NetWorkTool.getCheckCode(
            (res) => {
                if (res && res.data && res.data.captcha) {if (this.callback && typeof this.callback === 'function') {
                        this.callback(res.data.captcha);
}
                                        
                }
            }, (err) => {
                console.log('code err:', err);
            }
        )
    }
}

export default class App extends Component {
    constructor(props) {
        super(props);
this.model = new testNetworkModel(this.getImage);
this.state = {
            imageUrl: '',
};
}
    model = null;
getImage = (sourceUrl) => {
        this.setState({
            imageUrl                            
    : sourceUrl,
        });
    };
render() {
    return (
      <View style={styles.container}>          <TouchableOpacity
activeOpacity={1.0}
              onPress={this.model.getCheckCode}
              style={styles.check_image_btn}
          >
              <Image
source=
   
                                {{ uri: this.state.imageUrl, cache: 'reload' }}
                  style={{ width: 140, height: 45 }}
                  resizeMode={'contain'}
              />
          </TouchableOpacity>
      </View>
    );
  }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324714657&siteId=291194637