[英]JSX element type 'string' is not a constructor function for JSX elements. Typescript
So i have been working on a reusable icon element that I can call in any class and just pass a string <Icon name="chat" />
that contain the specific colour of the icon.. This error is the result of the previous question I raised.. you can find the link below所以我一直在研究一个可重用的图标元素,我可以在任何 class 中调用它并传递一个包含图标特定颜色的字符串
<Icon name="chat" />
。这个错误是上一个问题的结果我提出了..你可以找到下面的链接
I raised a question early before this: 在此之前我很早就提出了一个问题:
icon.ts file图标.ts 文件
const iconsList = {
heart: '',
star: '',
like: '',
dislike: '',
flash: '',
marker: '',
filter: '',
user: '',
circle: '',
hashtag: '',
calendar: '',
chevronLeft: '',
optionsV: '',
optionsH: '',
chat: '',
explore: ''
};
interface Props{
name: keyof typeof iconsList;
}
const Icon = ({name }: Props) => {
let icon = iconsList[name];
icon = icon.substr(3);
icon = String.fromCharCode(parseInt(icon, 16));
return icon;
};
export default Icon;
profile.tsx个人资料.tsx
import React from 'react';
import styles from '../assets/styles';
import {
ScrollView,
View,
Text,
ImageBackground,
TouchableOpacity
} from 'react-native';
import Icon from '../components/Icon';
const Profile = () => {
return (
<TouchableOpacity>
<Text style={styles.topIconLeft}>
<Icon name="chevronLeft" />
</Text>
</TouchableOpacity>
);
}
This line of <Icon name="chevronLeft" />
complains with an error of "JSX element type 'string' is not a constructor function for JSX elements.ts(2605)这行
<Icon name="chevronLeft" />
报错“JSX element type 'string' is not a constructor function for JSX elements.ts(2605)
string
is not a valid return value for a react component. string
不是反应组件的有效返回值。 A react component always has to return a ReactDOM
element or a Fragment
, that implements the render
method.一个 react 组件总是必须返回一个
ReactDOM
元素或一个Fragment
,它实现了render
方法。 You can try你可以试试
const Icon : React.SFC<Props> = ({name }: Props) => {
let icon = iconsList[name];
icon = icon.substr(3);
icon = String.fromCharCode(parseInt(icon, 16));
return <>{icon}</>;
};
And actually it won't hurt if you renamed icon.ts
to icon.tsx
.实际上,如果您将
icon.tsx
重命名为icon.ts
也不会受到伤害。 Depending on your compiler settings the <>{icon}</>
part might not get recognized correctly as JSX markup.根据您的编译器设置,
<>{icon}</>
部分可能无法正确识别为 JSX 标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.