调用的api未读取对象



当我用console.log调用detail时,没有显示任何错误检查:console.log(coin.description.en(但是当我在render中编写此代码时,{coin.description.en}不起作用,并显示此错误:类型错误:无法访问属性"en";,coin.description未定义

import React, {useEffect, useState} from "react";
import {connect} from 'react-redux';
import axios from 'axios';
function CoinDetail({...props}) {
const [coin,setCoin] = useState([]);
useEffect(() => {
getCoinDetail(API_URL+'ripple');
},[]);
const getCoinDetail = (id) => {
axios.get(id).then(res => {
setCoin(res.data);
console.log(res.data);
});
}

return (
<>
Id: {coin.id}<br/>
Name: {coin.name}<br/>
Symbol: {coin.symbol}<br/>
Description: {coin.description.en}<br/>
</>
);
}
function mapStateToProps(state,ownProps) {
const coinId = ownProps.match.params.coinId;
return {
coinId
}
}
export default connect(mapStateToProps)(CoinDetail);

尝试用{coin.description && coin.description.en}代替{coin.description.en}

相关内容

  • 没有找到相关文章

最新更新