Ema*_*ele 6 javascript reactjs styled-components
您好,我刚开始styled-components在我的项目中使用。我的工作环境是:
1) npm version 6.12
2) Node.js version 12.13
3) VS Code
styled-components从官方文档安装后:
sudo npm install --save styled-components
我的项目不再编译,抛出以下错误:
错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
检查 的渲染方法
Home。
到目前为止我做了什么:
1)根据调试器的建议,我仔细检查了我的Home page并尝试查看是否忘记了import该组件。但是,它已正确导入,但仍然无法正常工作。
2)始终按照调试器的建议检查是否存在无效的类型元素。但是我认为语法是正确的。为了确保这一点,我查阅了有关如何正确引用这些类型组件的官方文档。不幸的是它也没有奏效。
3)经过更多研究,我发现了这篇有用的帖子。似乎这可能是由于对包含的引用不准确。但是,在我安装styled-components. 所有import/export我都以同样的方式做的。
我提供了以下出现错误的代码片段:
StyledHero.js
import styled from "styled-components"
const SimpleButton = styled.button`
color:red;
background:green;
`;
export default SimpleButton;
Run Code Online (Sandbox Code Playgroud)
主页.js
import React from 'react'
import Hero from "../components/Hero"
import Banner from "../components/Banner"
import {Link} from "react-router-dom"
import Services from "../components/Services"
import FeaturedVessel from "../components/FeaturedVessels"
import Button from "../components/StyledHero"
export default function Home () {
return (
<>
<Hero hero="defaultHero">
<Banner title="Vessels" subtitle="Currently Tracked Vessels">
<Link to="/vessels" className="btn-primary">
Vessels
</Link>
</Banner>
</Hero>
<Services />
<FeaturedVessel />
<Button>hello</Button>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
我试过的附加选项:
作为尝试解决问题的最后说明,我认为我使用的组件styled-components应该以相同的名称导出,因此我还尝试使用内部创建的组件的确切名称导出它StyledHero.js:
StyledHero.js
import styled from "styled-components"
const SimpleButton = styled.button`
color:red;
background:green;
`;
export default SimpleButton;
Run Code Online (Sandbox Code Playgroud)
主页.js
import React from 'react'
import Hero from "../components/Hero"
import Banner from "../components/Banner"
import {Link} from "react-router-dom"
import Services from "../components/Services"
import FeaturedVessel from "../components/FeaturedVessels"
import SimpleButton from "../components/StyledHero"
export default function Home () {
return (
<>
<Hero hero="defaultHero">
<Banner title="Vessels" subtitle="Currently Tracked Vessels">
<Link to="/vessels" className="btn-primary">
Vessels
</Link>
</Banner>
</Hero>
<Services />
<FeaturedVessel />
<SimpleButton>hello</SimpleButton>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
如果在我下面有用package.json:
{
"name": "my-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-icons": "^3.8.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"styled-components": "^5.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以指出我正确的方向并了解问题所在吗?