react-native 点击按钮进行交互

在api/index.js里面封装了一个方法:
/ login登录接口
export const loginFromApiAsync = function (a,b) {
return fetch(URL+'/vhc/cust/api/login',{
method: "POST",
headers: {
Accept: "application/json",
"Content-type": "application/json",
"X-App-Platform": "app"
},
body: JSON.stringify({
account: a,
password: b
})

})
    .then(res => res.json())
    .then(res => {
        return res;
    })
    .catch(err => {
        console.log(err);
    })

};

在登录页面引入后使用
import {loginFromApiAsync} from "../api";

export default class Login extends Component {

constructor(props){
    super(porps);
    this.state = {
        account:''
    }
}

//定义一个方法
_loginFromApiAsync(){

//要获取state里面的数据account, 需要render里面使用函数时绑定this。如果不绑定this,this.state.account取不到
let a = this.state.account;

    loginFromApiAsync()
        .then( data => {
            //data成功后返回的数据
        }
    )
}

render(){
    <Text
        onPress = { this._loginFromApiAsync.bind(this)_}
    >点击</Text>
}

}

原文地址:https://www.cnblogs.com/xk-g/p/9519133.html