首页 > 解决方案 > src\components\Home.js Line 6:10: 'Container' is not defined react/jsx-no-undef

问题描述

import React from 'react'
import styled from "styled-components";

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

export default Home

错误来了

src\components\Home.js Line 6:10: 'Container' is not defined react/jsx-no-undef

标签: javascriptreactjsecmascript-2016

解决方案


您需要先定义容器,例如:

import React from 'react'
import styled from "styled-components";

const Container = styled.Text`
 font-size: 42;
`;

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

export default Home

如果您有一个Container已经创建并导出的默认值,则按如下方式导入它

import React from 'react'
import styled from "styled-components";
import Container from "file-path";

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

export default Home

如果将 Container 导出为常量,则需要导入为

import React from 'react'
import styled from "styled-components";
import { Container } from "file-path";

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

export default Home

更多请参考这里


推荐阅读