反应一下,从 API 获取会记录数据,但渲染它没有?



所以我有Django作为后端,并设置了一些基本的API与django_rest_framework。它完全可以工作,我可以获取数据并注销,但当我试图…我无法解释,我只能粘贴代码…如果需要任何进一步的信息,请随时询问,我会尽可能快地评论它。我现在不知道该怎么办,我已经挣扎了好几个小时了。

我在这里包含了所有的代码:https://pastebin.com/TfasCaNE(因为它很长)

{
posts.map(post =>{
console.log("log: ", post.id); // this works, it gets logged out correctly, for e.g., if there are 2 posts, 2 and 1 gets logged out.
})
}

{
posts.map(post =>{
<Grid>Hello</Grid> // this doesn't work, it doesn't do anything, not even an error, nothing
})
}

为什么会这样?

编辑:注意交换"网格";div"也没用。这只是一个MaterialUI库组件

您将Grid组件封装在代码块中,该代码块不返回任何内容。

去掉花括号:

posts.map(post => <Grid>Hello</Grid>)

或者在块内添加return关键字:

posts.map(post => {return <Grid>Hello</Grid>})

如果没有额外的映射逻辑,则首选前者。

最新更新