嗨,我知道这个问题有点愚蠢,但是我正在学习使用带有钩子的本机响应,有些事情使我难以理解。 我用axios进行了这个api调用
const getRFC = ({vLastName,vSecondLastName,vName,vSecondName,vBirthDate}) => {
axios. post(`http://exitusdesarrollo.eastus2.cloudapp.azure.com/AppForceControllers/controllers/GetRfcController.php`, { vName, vSecondName, vLastName, vSecondLastName, vBirthDate })
.then(res => {
console.log(res.data.resultRFC);
})
}
是的,控制台日志中会打印我需要的内容,所以我不知道该怎么做是如何在函数外使用res.data.resultRFC。 通常是
`const RFC= res.data.resultRFC;
this.setState({ RFC});`
但是由于即时通讯使用钩子会抛出错误,有什么建议吗?
答案 0 :(得分:2)
在函数中使用useState
:
import React, { useState } from 'react';
// inside your component
const [rfc, setRfc] = useState(null);
// inside your axios callback
setRfc(res.data.resultRFC);
// later in your component / render you can use rfc
{rfc && <Text>{rfc}</Text>}