我已经在react-native中成功设置了样式化组件,但现在使用的是react-native-web,在这个非常简单的示例中,无法使样式化组件在Web上运行:
import * as React from 'react';
import styled from 'styled-components';
export default class View extends React.PureComponent {
public render() {
return (
<Container>
<h1>Hey</h1>
</Container>
);
}
}
const Container = styled.div;
我收到此错误:
Type error: JSX element type 'StyledComponentClass<DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>, a...' is not a constructor function for JSX elements.
Property 'render' is missing in type 'StyledComponentClass<DetailedHTMLProps<HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>, a...'. TS2605
> 11 | <Container>
这是我的tsconfig.json:
"target": "es6",
"jsx": "preserve",
"module": "esnext",
"sourceMap": true,
"outDir": "dist",
"types": [
"react",
"react-native",
"jest"
],
"skipLibCheck": true,
"lib": [
"es2015.promise",
"esnext",
"dom"
],
"alwaysStrict": false,
"downlevelIteration": true,
"strict": false,
"strictNullChecks": false,
"allowJs": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
还有我的package.json:
...
"devDependencies": {
...
"@types/react": "^16.7.20",
"@types/react-dom": "^16.7.20",
"@types/react-native": "^0.52.25",
"typescript": "^2.9.2"
},
"dependencies": {
...
"react": "16.3.1",
"react-dom": "^16.7.0",
"styled-components": "^3.4.5",
}
我尝试rm -rf node_modules; rm package-lock.json; npm install
无济于事。
这里可能缺少一些简单的东西,但是有人有什么想法吗?不知道我还能提供什么其他信息。
答案 0 :(得分:1)
您将要使用React Native组件而不是标准HTML标签。
您将使用styled.div
而不是styled.View
。另外,您还必须使用react native的Text
组件来代替将保存文本数据(例如<H1>
)的标准HTML标签。
因此,这就是您的代码应转换为的
import * as React from 'react'
import { Text } from 'react-native'
import styled from 'styled-components/native'
export default class View extends React.PureComponent {
render() {
return(
<Container>
<Text>Hey</Text>
</Container>
)
}
}
const Container = styled.View