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

React报'Container' is not defined react/jsx-no-undef错误如何解决?

错误产生原因

你在JSX语法中直接使用了<Container>标签,但没有提前使用styled-components的API定义该样式组件,触发了ESLint的react/jsx-no-undef(禁止使用未定义变量)校验规则,因此抛出错误。

修复方法
  • 先使用styledAPI完成Container样式组件的定义后再在组件内调用即可,修改后的完整代码如下:
import React from 'react'
import styled from "styled-components";

// 新增Container样式组件的定义
const Container = styled.div`
  // 此处可根据需求添加自定义样式
  padding: 20px;
  background: #fff;
`

function Home() {
    return (
        <Container>
            home
        </Container>
    )
}

export default Home
  • 若你需要Container对应其他HTML原生标签,只需替换styled.div为对应标签即可,比如styled.main、styled.section等均为合法写法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 00:15:03