2 回答

TA貢獻1818條經驗 獲得超8個贊
您在return函數調用中缺少 a - 您是從內部then而不是外部承諾返回
function _toRoomName(title) {
return axios({
method: "POST",
url:"hashroomname.php",
data: {
roomname: title
}
}).then((response) => {
console.log(response.data);
return response.data;
}).catch((error) => {
return error;
});
}
但這不會真正起作用。您不能將承諾嵌入到 中<Text>,您需要將其提升到外部狀態
例如
const [ data, setData ] = useState(null)
useEffect(() => {
_toRoomName(title)
.then(response => {
setData(response)
})
}, [])
return (
<Text className = 'titled'>
{data}
</Text>
)
nowdata在加載<Text>之前為空,在有數據之前為空

TA貢獻1852條經驗 獲得超1個贊
解決方案是:1)使您的功能異步。
async function _toRoomName(title) {
const axios = require('axios');
axios({
method: "POST",
url:"hashroomname.php",
data: {
roomname: title
}
}).then((response) => {
console.log(response.data);
return response.data;
}).catch((error) => {
return error;
});
}
2)將您的函數移動到 componentDidMount() 并將結果存儲在狀態中。在渲染方法中使用該狀態。
componentDidMount() {
const axios = require('axios');
axios({
method: "POST",
url:"hashroomname.php",
data: {
roomname: title
}
}).then((response) => {
this.setState({state:response.data})
}).catch((error) => {
return error;
});
}
添加回答
舉報