You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React非布尔属性`item`赋值`true`警告排查及Heroku部署异常解决

解决React DevTools警告:Received true for a non-boolean attribute item

问题原因

你使用的是Material-UI的Grid组件,item属性只能作为设置了container属性的Grid的直接子元素来使用。你的代码里存在两处不符合这个规则的地方:

  • <Grid className="topGrid"> 没有添加container属性,但它的子Grid都加了item
  • <Grid className="postList-grid"> 同样没有container属性,却包含带item的子Grid

React会把组件无法识别的属性传递给底层DOM元素(这里是div),而标准HTML的div没有布尔类型的item属性,当你只写item(等价于item={true})时,就会触发这个警告。

修复步骤

给包含item子元素的父Grid添加container属性即可:

  1. 修改topGrid对应的Grid:
<Grid className="topGrid" container>
  1. 修改postList-grid对应的Grid:
<Grid className="postList-grid" container>

修复后的关键代码片段:

<Grid className="homeContainer" container spacing={1}>
  <Grid className="topGrid" container>
    <Grid className="imageContainer" item xs={4} sx={{ }}>
      <img className="cctLogoLarge" src={cctlogo1} alt="outline of horse with parliament buildings in background"></img>
    </Grid>
    <Grid className="introContainer" item xs={12}>
      <p>Welcome to Cap City Trotters! The CCT was created in 2019 by KMAC, Lynch, Bruster, and Damarts. Our Routes include several loops in both Ottawa and Gatineau.
      We are always looking for new members so if you want to join you can take a look at some of our main routes and signup if you like what you see!</p>
    </Grid>
  </Grid>

  <Grid>
    <Grid className="postList-grid" container>
      <Grid item xs={6} sx={{ bgcolor: ""}} className="recentPosts">
        {loading ? (
          <div>Loading...</div>
        ) : (
          <PostList posts={posts}/>
        )}
      </Grid>
      {/* 其余代码保持不变 */}
    </Grid>
  </Grid>
</Grid>

关于Heroku显示不一致的说明

这个警告本身通常不会直接导致部署后显示异常,但修复它能排除一个潜在问题。如果修复后Heroku的问题仍存在,建议检查:

  • 静态资源路径(比如图片cctlogo1的引入是否正确,部署后路径是否可访问)
  • 环境变量配置是否和本地一致
  • Heroku的构建缓存,尝试重新部署时清除缓存

内容的提问来源于stack exchange,提问作者damarts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 11:10:51