【发布时间】:2025-11-29 00:05:02
【问题描述】:
我正在尝试从 API 检索一些数据。当我控制数据时,它工作正常:
import axios from 'axios';
export default function Model() {
const url = "api.blabla.com/blabla"
const [model, setModel] = useState()
useEffect(() => {
const axiosPosts = async () => {
const response = await axios(url)
setModel(response.data)
};
axiosPosts();
}, []);
console.log(model.slug) //prints the slug!!
return (
<div>
{model.slug} : {model.name} // TypeError slug undefined
</div>
)
这段代码有什么问题?
【问题讨论】:
-
长度在哪里?请注意,模型的 initial 状态确实是未定义的。
-
感谢您的编辑,您的问题已解决。
标签: javascript reactjs api axios use-effect